I am making an application which has about 5 pages along with a starting screen, I am aware that if I write finish()
in the onPause()
method the page will get destroyed once the user goes to the next page.
My requirement is such that I don't want the AppPage 1 to be destroyed till I reach the 3rd page of the application, but as soon as the user goes to 4th page of application, I want to destroy all the 1,2,3 AppPages of my application so that they cannot be accessed by clicking the back button and reaches directly to the starting screen of my application which I am not killing at the starting of the application.
So I want to ask is it possible to kill my application's 1,2,3 pages when the user clicks on the go to next page button of the 3rd page.
Thanks
==== Edit =====
Starting Screen -> AppPage1 -> AppPage2 --> AppPage3 --> AppPage4 (Kill AppPage1,2,3 here, so that if back is clicked user reaches starting sceen) --> Appage 5 (Kill AppPage4)
==== Edit 2 =====
AppPage1.java
public class AppPage1 extends Activity{
Button goToAppPage2;
BroadcastReceiver logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.apppage1);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.closing.application.pages.AppPage1");
logout = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
registerReceiver (logout, intentFilter);
goToAppPage2 = (Button) findViewById(R.id.goToAppPage2);
goToAppPage2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i1 = new Intent(AppPage1.this, AppPage2.class);
startActivity(i1);
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(logout);
}
}
AppPage2.java
public class AppPage2 extends Activity{
Button goToAppPage3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.apppage2);
Intent intent = new Intent("kill");
intent.setType("spartan!!!");
sendBroadcast(new Intent(this, AppPage1.class));
goToAppPage3 = (Button) findViewById(R.id.goToAppPage3);
goToAppPage3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i1 = new Intent(AppPage2.this, AppPage3.class);
startActivity(i1);
}
});
}
}
You can achieve this through a broadcast message. I am using it myself in the case where the Activities depend on the user to be logged-in, so when he logs out, all those Activities should be finished and only the login screen should remain.
First, register the broadcast in the activities that should be finished. If the number of activities is big, you can create a parent Activity from where the other can extends, so you don't have to repeat this code so many times:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.ACTION_LOGOUT");
BroadcastReceiver logout = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
registerReceiver (logout, intentFilter);
Don't forget to unregister it onDestroy():
unregisterReceiver(logout);
Send the broadcast when you wish to finish the previous activities:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.example.ACTION_LOGOUT");
sendBroadcast(broadcastIntent);