Venkat Reddy
Venkat Reddy

Reputation: 91

How to restart MAIN activity

I need the content on the MAIN activity to be updated when a child activity returns. i tried the following code inside the onActivityResult() method which works fine for other activities (non-MAIN) but not for MAIN activity.

if (resultCode == Activity.RESULT_OK){
    finish();
    startActivity(getIntent());
}

Any ideas on how to make the MAIN activity execute the OnCreate() again?

Thanks!

Upvotes: 2

Views: 4717

Answers (4)

Put the following code inside onBackPressed() method:

public void onBackPressed() {
    Intent intent= new Intent(MainActivity.this, MainActivity.class);
    startActivity(intent);
}

Where MainActivity.class is my launcher Activity. Now the activity will return to the same content it launches every time.

Upvotes: 0

akhaj
akhaj

Reputation: 1

Your code should look something like this:

Intent intent = new Intent(YourMainActivity.this, YourMainActivity.class);
startActivity(intent);
finish();

Upvotes: 0

Venkat Reddy
Venkat Reddy

Reputation: 91

The issue seems to that the setResult() is ignored if the current activity is restarted. See my other post.

Upvotes: 0

SRam
SRam

Reputation: 2828

write the thing on OnResume () method..it will execute always when u return back to main activity..for details go through activity life cycle..

Upvotes: 1

Related Questions