Sarim Sidd
Sarim Sidd

Reputation: 2176

Go back to the First/Main activity without reloading it

I am developing an android app. I need to call my MainActivity without reloading it as it has huge amount of data fetch from internet.

Suppose, I am on third activity now and I want to go back to MainActivity.

If I use:

Intent i = new Intent(Third.this,Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

it will load MainActivity but I don't want to reload it. like from Second Activity I call finish() and it does exactly want i need.

Upvotes: 6

Views: 10876

Answers (4)

Eng.Fouad
Eng.Fouad

Reputation: 117589

This is how to do it:

Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);

Upvotes: 16

Ravi1187342
Ravi1187342

Reputation: 1277

metntion it in your AndroidManifest.xml file

<activity android:name=".MyActivity"

              android:configChanges="keyboardHidden|orientation">

do nothing inside the method onResume() and onstart() when coming back to this activity

and try intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); instead of addFlags() method

Upvotes: 4

Ayham
Ayham

Reputation: 302

Try adding this to your manifest:

                  android:configChanges="keyboard|keyboardHidden|orientation">

add this line to the activity in your manifest like this:

       <activity android:name=".Main"
              android:label="@string/app_name"
              android:configChanges="keyboard|keyboardHidden|orientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

Upvotes: 1

Chintan Raghwani
Chintan Raghwani

Reputation: 3370

In Third activity when you want to go to first activity, put finish() mwthod there.

In Second activity, after onCreate() method, put @Override public void onResume(){ super.onResume(); finish(); }

I think this code will work for you, jus try it.

Upvotes: 0

Related Questions