Siva K
Siva K

Reputation: 4968

android clear particular activities from the stack without finish()

in my app i am having three main activities named as Home, List and Details

first time of my navigation is to be as follows Name -> List -> Details

In the Details page when the user selects any option then i am calling the same activity once again to show new Details. This goes on for n number of times.

In the Details page i have three important buttons 1. back, 2. List, 3. Home

My problem from Details page are

1. when List button is clicked i have to move on to the List Activity, and by pressing back button from List Activity i need to show Home page, not Details Page

2. when Home button is clicked i have to move on to the Home Activity, and by pressing back button from Home Activity i need the app to get closed.

3. back -> 
   a. when the user comes first time to the Details page, by pressing back button he needs to move to List Activity

   b. after loading the Details activity again and again, by pressing the back button user can move on to previous Details, here the condition is while starting the activity i should not give finish(), because this causes the data to get loaded of a long time

how to overcome the above issues

Upvotes: 0

Views: 614

Answers (2)

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

Now we'll see your issues one by one:

1. when List button is clicked i have to move on to the List Activity, and by pressing back button from List Activity i need to show Home page, not Details Page

Now you can achieve this by:

Do not call finish() for home or list activity whenever you go to any other activity.

In this way, Home activity will always be present on stack. So now, call startActivity for Home activity or List activity with flag set as FLAG_ACTIVITY_REORDER_TO_FRONT that should bring your activity to front.

2. when Home button is clicked i have to move on to the Home Activity, and by pressing back button from Home Activity i need the app to get closed.
3. back -> 
   a. when the user comes first time to the Details page, by pressing back button he needs to move to List Activity
b. after loading the Details activity again and again, by pressing the back button user can move on to previous Details, here the condition is while starting the activity i should not give finish(), because this causes the data to get loaded of a long time

Again this can be done by using same as startActivity for Home with flag set as above.

OR

use startActivityForResult() from details to any other activity. So, now when you want to go back to Home activity from (all) Details activity, and use setResult as ex.: RESULT_FINISH and in onActivityResult() check for this result code and if you receive this result code, call finish for details activity.

This way, all your Details activity will get finished.

And from Home activity. if you press finish(), your app will get closed (make sure all your activities are calling finish at some point of time.) since all your activities are getting finished.

EDIT:

When you go from Home > List > Details, your issue is:

1. when List button is clicked from details activity, i have to move on to the List Activity, and by pressing back button from List Activity i need to show Home page, not Details Page.

This will work, if you dont call finish() for any of the activity Home > List > Details.

Now, you can call Details from Details many times, for this, what I suggested, this is a dummy code:

    public class Details extends Activity{

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*
         * If user presses back button, handle it like this:
         */
        if(requestCode == REQUEST_DETAILS) {
            if(resultCode == RESULT_FINISH) {
                finish();
            }
        }
        // In this way all your Details Activity will get finished.
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        /*
         * If user presses back button, call following:
         * 
         * set some flag as TRUE, ex.: 
         * 
         * flagIsFinishing = true;
         * 
         * setResult(RESULT_FINISH);
         * finish()
         */
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * if(flagIsFinishing) { 
         *      setResult(RESULT_FINISH);
         *      finish();
         * }
         * 
         */
    }

    @Override
    protected void onResume() {
        super.onResume();
        /*
         * On selecting any option call Details activity like
         * 
         * startActivityForResult(intent, REQUEST_DETAILS);
         */
    }
}

Upvotes: 1

Siva K
Siva K

Reputation: 4968

i solved the issue by using a flag in OnResume()

In Details page when List button is clicked i am calling onBackPressed();, at that time i am setting a boolean flag to be true.

by default if the Detail Activity is called for the first time it gets back to the previous activity ie List Activity.

If the Detail activity is called for n number of times, and the List button is clicked, with boolean value true, in onResume i have called the function onBackPressed(); and it gets moved to the List Activity

Upvotes: 0

Related Questions