Darshan Dhoriya
Darshan Dhoriya

Reputation: 1014

How can we clear All the Data of Previous Activity in Android

I just want to Clear all the data of Previous Activity. Means when i will go back to previous activity it need to call all the asyncatsk again as a fresh new activity. Because when i was goes in a next activity i was doing some changes in data sequence but i will come back i need to call agin asynctask for original data

So anyone have idea about it ??

and yes i just want to clarify that Below's Function is not working

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 0

Views: 2876

Answers (2)

Ryan Pierce
Ryan Pierce

Reputation: 1633

There are a couple options depending on your situation.

First, you could try clearing your savedInstanceState like this:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(null);
}

Or like this:

protected void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.clear();
}

Second, if you're hoping to have this specifically on the "backpressed" event, you could override onBackPressed to create a new instance of your activity, rather than the default of returning to the activity with its saved state.

@Override
public void onBackPressed(){
    //super.onBackPressed(); // EXCLUDE this line
    Intent intent = new Intent(this,TheNextActivity.class);
    startActivity(intent);
}

@iguarna 's answer is also something to try, it all depends on the situation and what you're hoping to accomplish

Upvotes: 1

ig343
ig343

Reputation: 277

If you want to re-initialize your activity in any way when you come back to it, just reset the values you want in onResume().

If that is not what you were looking for, please clarify your question.

Upvotes: 0

Related Questions