Unit-2: � Android Application Design and Resources � �
Activity Life Cycle
Activity Life Cycle
Activity Life Cycle - Methods
onCreate()
onStart()
Activity Life Cycle - Methods
onResume()
onPause()
Activity Life Cycle - Methods
onStop()
onDestroy()
Fragments
Intents
Explicit intents
Implicit intents
EXPLICIT INTENTS
EXAMPLE EXPLICIT INTENT
Intent i = new Intent(this, SecondActivity.class); startActivity(i);
IMPLICIT INTENTS
EXAMPLE - IMPLICIT INTENT
String url = "http://www.vogella.com";
Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url));
startActivity(i);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com"));
startActivity(i);
IMPLICIT INTENTS
IMPLICIT INTENTS
IMPLICIT INTENTS
IMPLICIT INTENTS
INTENT FILTER
INTENT FILTER
INTENT FILTER
INTENT FILTER
INTENT FILTER -- (MANIFEST FILE)
<activity android:name="ShareActivity"> <intent-filter> <action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lightcone.sharingintents" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<activity android:name=".SharingIntents" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyLittleBrowser" android:label="@string/little_browser_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity>
</application>
</manifest>
Example
Here we have only one button,
<?xml version="1.0" encoding="utf-8"?>�<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"�xmlns:tools="http://schemas.android.com/tools".......>�<Button�android:id="@+id/btn"�android:layout_width="wrap_content"�android:layout_height="wrap_content"�android:text="Go to another activity"�android:layout_centerVertical="true"�android:layout_centerHorizontal="true" />�</RelativeLayout>
activity_main.xml
Package androidhunger.activityfragmentintentdemo;
import android.content.Intent;�...
public class MainActivity extends AppCompatActivity {
@Override�protected void onCreate(Bundle savedInstanceState) {�super.onCreate(savedInstanceState);�setContentView(R.layout.activity_main);�Button btn = (Button) findViewById(R.id.btn);�btn.setOnClickListener(new View.OnClickListener() {� @Override� public void onClick(View v) {� startActivity(new Intent(MainActivity.this, SecondActivity.class));� }� });� }�}
MainActivity.java
<?xml version="1.0" encoding="utf-8"?>�<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"�...>
<TextView�android:layout_width="wrap_content"�android:layout_height="wrap_content"�android:textAppearance="?android:attr/textAppearanceLarge"�android:text="This is the Top Fragment"�android:id="@+id/textView"�android:layout_gravity="center" />�</LinearLayout>
top_fragment.xml and bottom_fragment.xml
package androidhunger.activityfragmentintentdemo;
import android.app.Fragment;�....
public class TopFragment extends Fragment{�@Override�public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {�View view = inflater.inflate(R.layout.top_fragment,container,false);�return view;�}�}
TopFragment.java and BottomFragment.java
<?xml version="1.0" encoding="utf-8"?>�<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"�xmlns:tools="http://schemas.android.com/tools"�android:layout_width="match_parent"�android:layout_height="match_parent"�.....">
<fragment�android:layout_width="wrap_content"�android:layout_height="wrap_content"�android:name="androidhunger.activityfragmentintentdemo.TopFragment"�android:id="@+id/fragment"�android:layout_alignParentTop="true"�android:layout_centerHorizontal="true"�tools:layout="@layout/top fragment" />
<fragment�android:layout_width="wrap_content"�android:layout_height="wrap_content"�android:name="androidhunger.activityfragmentintentdemo.BottomFragment"�android:id="@+id/fragment2"�android:layout_alignParentBottom="true"�android:layout_centerHorizontal="true"�tools:layout="@layout/bottom fragment" />�</RelativeLayout>
TopFragment.java and BottomFragment.java
OUTPUT:
THANK YOU….