0

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

        }
    });    
} 
}
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Sumit
  • 103
  • 1
  • 10
  • your actual requirement is when u are in fourth activity in Activity stack then you dont want that ur application supports back to activity 3 to activity 2 to activity 1 on click of back button.?? – Gaurav Chawla Jun 09 '12 at 13:40
  • no I want those pages to be closed, completely killed .... like using the `AppPage3.finish()`, `AppPage2.finish()`, `Appage1.finish()` on the respective page will kill these pages but I want them to be killed here after I have reached AppPage4 – Sumit Jun 10 '12 at 09:00
  • 1
    The error is in AppPage2. When you send the broadcast message, use exactly the code I gave you. You just have to replace "com.example.ACTION_LOGOUT" with your "com.closing.application.pages.AppPage1". The action name should be the same.class. Read about broadcast messages in Android if you don't understand the behavior. I hope it helps! :) – MRD Jun 11 '12 at 10:00
  • oh man ..... I got confused between two pages I was following :-P ... my bad ... thanks a lot for the quick reply and helping me :-) – Sumit Jun 11 '12 at 10:23

4 Answers4

2

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);
MRD
  • 7,146
  • 1
  • 21
  • 21
  • Sorry I am new and is unable to implement this in my application, requirement for which I have mentioned in the question above, can you please give me a more elaborate answer. Thanks. – Sumit Jun 11 '12 at 07:57
  • 1
    Add the first part, where the broadcast receiver is created to your AppPage1, AppPage2 and AppPage 3 onCreate() method. In the same activities, unregister the receiver on the onDestroy() method. Then, from your AppPage4, send the broadcast message with the last part and the Activities 1,2 and 3 will be finished automatically, because they will receive the broadcast message and they will execute the finish() statement. – MRD Jun 11 '12 at 08:01
  • still unable to make it run :-( .... I am attaching the code above .. I tried to close AppPage1 from AppPage2 using this ... please have a look at the code and tell me what blunder am I making ! – Sumit Jun 11 '12 at 09:50
0

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 .

abbas.aniefa
  • 2,855
  • 21
  • 30
  • 1
    If I understood well, he wants the pages 1,2,3 to not be reachable. When launching the 4th activity, if he uses this flag, the home screen would not be reachable as well. – MRD Jun 09 '12 at 14:10
0
YourActivityName.this.finish();
                 ^^^^

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

MAC
  • 15,799
  • 8
  • 54
  • 95
0

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?

Community
  • 1
  • 1
Anearion
  • 103
  • 1
  • 10