3

Suppose there is a button on the homepage of my Activity.

When I press this button, it goes to Activity A

in Activity A , it automatically goes to Activity B using Intents and startActivityForResult();

in Activity B , it automatically goes to Activity C using Intents and startActivityForResul();

in Activity B, it automatically starts the intent to go BarCode scanner activity and get results.

My problem is when I press a button to return to my home page, it requires pressing 4 times go back to my homepage

If I want to press back to my homepage at one time, how to finish such series of activities?

1
  • The back button is supposed to work this way. You probably want to implement the Up button (Honeycomb and later, unless you use something like ActionBarSherlock) instead. Jul 14, 2012 at 4:46

4 Answers 4

4

Don't Use/override onBackPressed() anywhere in your program

just add the following method in the parent activity such as Activity A and B

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

Start Activity on your onClick method using

    startActivityForResult(intentName, 0);

In Child Activity you use/override onStop() method

    @Override
protected void onStop()
{
    super.onStop();

}
1
  • I can not understand what you did here? Could you please explain?
    – Darpan
    Jul 31, 2014 at 10:22
2

You can override the following method to return to homepage

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }

Within that method create the intent for home activity with the following tag

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

The document says

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

But what i am wondering is, then why you are starting the activity for result, since you are not going to return to previous activity.

0
finishActivity(requestCode)

use above code in last activity so that after pressing back button it will close all the activity that are started

requestcode is a code which you used in first activity to start activity B

0

you may try this

    Intent intent = new Intent(currentActivity.this, otherActivity.class); // instead of otherActivity use your  activity
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);

And, in your other onCreate() use below code -

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.