paola91
paola91

Reputation: 369

Android activity still displays old content

I have an app in which the navigation among the activities is basically organized as follows:

MainActivity -> Activity1 -> Activity2.

In MainActivity the user opens a file, which is displayed/explored in Activity1. In Activity2 further information about the file is shown on the basis of the user's action in Activity1.

Activity1 has android:launchMode="singleTop"so navigating back from Activity2 to Activity1 preserves its status.

Now, I inserted in both Activity1 and Activity2 an "Exit" button, to come back to MainActivity and open a new file.

Unfortunately, when I open the new file, Activity1 displays overlapped information about the new file and the previous one. How could I avoid Activity1 to keep track of previous instance when I start it from MainActivity? Thanks in advance.

Upvotes: 0

Views: 69

Answers (1)

Ismail
Ismail

Reputation: 159

why don't you just start the activity and call finish() on Listeners

// MainActivity
Intent intent = new Intent(this, YourActivity.class);
this.startActivity(intent);

//Activity 1
finish();

or if you want to handle both in MainActivity you could do the following, you can also customize it for providing parameter maps with putExtra() method.

// MainActivity  
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == 1) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                // Do your stuff here
            }
        }
        if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                // Do your stuff here
            }
        }
    }  

// Your listener for starting another activity, use in Main Activity
Intent intent = new Intent(this, Activity1.class);     
startActivityForResult(intent,1);

//Activity1
    // start and exit, if you wanna handle Activity2 from Activity1 you need to override onActivityResult for it
    Intent intent = new Intent(this, Activity2.class); 
    Intent goingBack= new Intent();
    setResult(RESULT_OK,goingBack);   
    startActivityForResult(intent,2);
    finish();


//Activity2
    //exit listener
    Intent goingBack= new Intent();
    setResult(RESULT_OK,goingBack);
    finish(); 

Upvotes: 1

Related Questions