Question

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);

        }
    });    
} 
}
Était-ce utile?

La solution

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);

Autres conseils

YourActivityName.this.finish();
                 ^^^^

try this this will not allow user to go back to that activity

Try this,

Use startActivityForResult(intent,0) to call the Activity 2 from Activity 1 and write following code in your Activity 1

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 0:
            if (resultCode == RESULT_OK) {
                finish();
            }
        }
    }

Do similarly in Activity 2 and call Activity 3 using startActivityForResult.

Finally form Activity 3 before calling Activity 4 you should call just finish() . If you call finish() in Activity 3, callback method onActivityResult written in previous Activity will get executed and it will finish itself .

you can override the onBackPressed() method in the 4th page, so that when an user press the back key he will be forced to the main page, or whatever you need.

 public void onBackPressed() {
        //code here, like calling back the main page of your app
    }

Lurking around i found that question, maybe the third answer can help you:

How to close all the activities of my application?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top