Activity A starts activity B with no flags. The stack is now A-B with B on top. B starts activity A with FLAG_ACTIVITY_REORDER_TO_FRONT (the only flag). I would expect the stack to now be B-A. However, when the back button is pressed at this point it returns to the home screen. Here I would expect Activity B to be brought to the front. Upon clicking on the launcher icon again, the app opens with B as the running activity and nothing in the stack.
Launchmode is standard (default) in the manifest.
Is this the expected behavior and I'm just not understanding it properly?
EDIT: I have created a test project with no confounding factors and still see the same behavior. I just don't understand it, it doesn't seem to be per the documentation.
EDIT: To me this behavior seems to be a BUG in the framework, see my comment on answer below. I need a workaround.
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClickBtn(View view)
{
Intent flowIntent = new Intent(this, SecondActivity.class);
startActivity(flowIntent);
}
}
public class SecondActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClickBtn(View view)
{
Intent flowIntent = new Intent(this, MainActivity.class);
flowIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(flowIntent);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tester"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.tester.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.tester.SecondActivity" />
</application>
</manifest>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…