BackStack Example Output :
|
Output of this example |
|
Project Structure |
Step 1 :
Create 3 Activities :
1) ActivityB
2) ActivityC
3) ActivityD
Step 2:
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity A"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Activity->B"
android:onClick="onNextActivity"/>
</LinearLayout>
MainActivity.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onNextActivity (View view)
{
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
}
Step 3 :
activity_b
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity B"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onNextActivity"
android:text="Next Activity -> C" />
</LinearLayout>
ActivityB.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
public class ActivityB extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_b);
}
public void onNextActivity (View view)
{
Intent intent = new Intent(this, ActivityC.class);
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
}
Step 4 :
activity_c
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity C"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onNextActivity"
android:text="Next Activity -> D" />
</LinearLayout>
ActivityC.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
public class ActivityC extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_c);
}
public void onNextActivity (View view)
{
Intent intent = new Intent(this, ActivityD.class);
startActivity(intent);
}
public void onBackPressed() {
Intent myIntent = new Intent(this,MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// clear the backstack
startActivity(myIntent);
finish();
return;
}
}
Step 5 :
activity_d
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity D"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onExit"
android:text="Exit" />
</LinearLayout>
ActivityD.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
public class ActivityD extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_d);
}
public void onExit (View view)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
ActivityD.this.finish();
}
public void onBackPressed() {
Intent myIntent = new Intent(this,ActivityB.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// clear the backstack
startActivity(myIntent);
finish();
return;
}
}
Step 6 :
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pratik.backstackdemo" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".ActivityB"
android:label="@string/title_activity_activity_b" >
</activity>
<activity
android:name=".ActivityC"
android:label="@string/title_activity_activity_c" >
</activity>
<activity
android:name=".ActivityD"
android:label="@string/title_activity_activity_d" >
</activity>
</application>
</manifest>
Step 7 :
Run the app and enjoy it....
Download This Project from below link :
No comments:
Post a Comment