-1

I have an application with three layouts.

According to the android documentation if you use the FLAG_ACTIVITY_CLEAR_TOP flag in the intent to return to the top activity, and the activity has it's launch mode set to 'standard' you should get a new instance of the activity. This will load the different layout that was set in preferences.

I get this behaviour in the android emulator running 4.2.2 without using any flags except FLAG_ACTIVITY_CLEAR_TOP. In my Nexus 7 also running 4.2.2 I never get a new instance and get the old layout unless I quit and restart the app.

I have tried adding 'android:launchMode="standard"' to the manifest and the FLAG_ACTIVITY_NEW_TASK flag to the intent with no change.

Here is an extract from the manifest and the code snippet that should restart it.

<activity
android:name="org.billthefarmer.accordion.MainActivity"
android:label="@string/app_name"
android:launchMode="standard"
android:screenOrientation="portrait" >
  <intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

Code from the class:

case android.R.id.home:
    // app icon in action bar clicked; go home
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    return true;

Is there a better way of achieving this or am I missing something?

Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85
Bill Farmer
  • 271
  • 3
  • 5
  • you cannot override the default behaviour of home button on 4.0 (i guess) and above. its for security reasons. – Raghunandan May 23 '13 at 12:44
  • Having spent a while writing this I tried changing the code to two addFlags() statements instead of one with ored flags and it worked. Even more inexplicable. – Bill Farmer May 23 '13 at 12:47
  • `FLAG_ACTIVITY_CLEAR_TOP` closes all activities on top of now launching activity (i.e., in your this particular case, closes all activities on top of MainActivity and does not reset MainActivity itself). – ozbek May 23 '13 at 13:15

1 Answers1

0

Try adding this method to your activity code:

public void reload() {

    Intent intent = getIntent();
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();

    overridePendingTransition(0, 0);
    startActivity(intent);
}

Taken from here

Community
  • 1
  • 1
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216