wawanopoulos
wawanopoulos

Reputation: 9804

Android: Clear old activity instance from history stack

I have the following simple scenario:

Problem: From now, when I click on the Back button, the ListCarsActivity is displayed a second time.

How to clear the old entry of ListCarsActivity from the History stack?

enter image description here

Upvotes: 2

Views: 191

Answers (5)

Komal
Komal

Reputation: 338

Don't call ListCarsActivity again when user add the car button just put finish() Because your first activity, you've just started the activity ListCarsActivity.kt not the finished yet so, it's already in stack. If you will call again ListCarsActivity.kt then in your stack your activity opened two times.

Upvotes: 0

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

Call your AddCarActivity.kt Activity like below inside your ListCarsActivity.kt

val intent = Intent(mContext, AddCarActivity.class);
startActivityForResult(intent, 1); //Use any request code

Now for the above code what startActivityForResult do is that it is an intent to receive data back from some other activity or source(like camera, contacts, etc)

Now when all the work is done in your AddCarActivity.kt you can all the below intent to get the result back

                Intent intent = getIntent();
                intent.putExtra("key", data); // if you wanted to get data 
                setResult(RESULT_OK, intent);
                finish();

And inside your ListCarsActivity.kt you need to add onActivityResult which will get the data from AddCarActivity.kt

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

        if (resultCode == RESULT_OK) {
            if (requestCode == 1) { //Your request code here
                Object obj = data.getParcelableExtra("key");
                 //Do as per your needs

            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

With this, if you wanted to return the newly added data back in the first activity it can be fetched without the usage of any third-party libs.

Try it and do let me know if it worked for you.

Upvotes: 2

Atish Agrawal
Atish Agrawal

Reputation: 2877

Your approach is valid and will work as you want. However, I would like to a bit more information here.

In your ListCarsActivity, on clicking the floatingButton, you are starting a new activity AddCarActivity. Once done, on clicking the button "Add Car", you can rather do your save/post logic and close (finish()) this (AddCarActivity).

Doing this will prevent you from relaunching the same activity (ListCarsActivity) again. Also, If you close (finish()) the ListCarsActivity and if the user presses back from the AddCarActivity, the app will close (considering there are only two activities). This is definitely what you don't want.

Rather, you can implement onResume method in your ListCarsActivity to refresh the list with new and updated data. This same function will be called once the user clicks on "Add Car" and the AddCarActivity is closed. Hence, there would be no need to startActivity again.

Upvotes: 0

Gollard
Gollard

Reputation: 34

You should run AddCarActivity with startActivityForResult method, and then you have to set result with setResult method in AddCarActivity which returns you to ListCarsActivity without starting it again

Official documentation: https://developer.android.com/training/basics/intents/result

https://developer.android.com/reference/android/app/Activity#setResult(int,%20android.content.Intent)

Upvotes: 0

Prakash Reddy
Prakash Reddy

Reputation: 1002

use finish() after startActivity(intent) to finish the activity in which you are starting another activity

Upvotes: 0

Related Questions