I have an activity A, which displays three fragments according to the groupName field. I have a menu button which lauches another activity B. When I click this button and then click the back button, groupName is null in activity A. I have also tried saving and restoring using savedInstanceState, but to no avail. I do not believe this is necessary, because activity A is never destroyed.
I have checked the Android manifest, both activities are properly registered and activity A is parent of activity B.
public class GroupOverviewActivity extends AppCompatActivity implements MyTasksInteractionListener, OnGroupTasksFragmentInteractionListener {
private static final String TAG = "GroupOverviewActivity";
private String groupName;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_overview);
if (savedInstanceState != null) {
groupName = savedInstanceState.getString("groupname");
} else if (getIntent() != null && getIntent().getExtras() != null) {
groupName = getIntent().getExtras().getString("groupname");
}
}
@Override
protected void onResume() {
super.onResume();
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
final ActionBar bar = getSupportActionBar();
if (bar != null) {
bar.setDisplayHomeAsUpEnabled(true);
bar.setTitle(groupName);
}
getSupportFragmentManager().beginTransaction()
.add(R.id.group_overview_fragment, GroupMyTasksFragment.newInstance(groupName), null)
.disallowAddToBackStack()
.commit();
final BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.item_activity:
getSupportFragmentManager().beginTransaction()
.replace(R.id.group_overview_fragment, GroupActivityFragment.newInstance(groupName))
.disallowAddToBackStack()
.commit();
break;
case R.id.item_schedule:
getSupportFragmentManager().beginTransaction()
.replace(R.id.group_overview_fragment, GroupScheduleFragment.newInstance(groupName))
.disallowAddToBackStack()
.commit();
break;
case R.id.item_tasks:
getSupportFragmentManager().beginTransaction()
.replace(R.id.group_overview_fragment, GroupMyTasksFragment.newInstance(groupName))
.disallowAddToBackStack()
.commit();
break;
}
return false;
}
});
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
outState.putString("groupname", groupName);
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.group_overview, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (item.getItemId() == R.id.item_members) {
final Intent intent = new Intent(this, GroupMembersActivity.class);
intent.putExtra(GroupMembersActivity.ARG_PARAM1, groupName);
startActivity(intent);
}
return true;
}
The layout of the activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="giphouse.nl.proprapp.ui.group.overview.GroupOverviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ProprTheme.AppBarOverlay"
android:id="@+id/appBarLayout">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ProprTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appBarLayout"
android:id="@+id/group_overview_fragment">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/nav_item_color_state"
app:itemTextColor="@drawable/nav_item_color_state"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation_group_overview">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
The way the GroupOverviewActivity is started:
final Intent intent = new Intent(context, GroupOverviewActivity.class);
intent.putExtra("groupname", dto.getGroupName());
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
context.startActivity(intent);
}
});
The relevant logging. Each statement is appended by the value of goupname:
11-20 08:32:16.181 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onCreate DEVTEAM
11-20 08:32:16.185 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onStart DEVTEAM
11-20 08:32:16.222 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onResume DEVTEAM
When the menu item is clicked launching activity B:
11-20 08:32:22.766 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: Saving instance state DEVTEAM
11-20 08:32:22.038 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onPause DEVTEAM
After activity B is launched and visible:
11-20 08:32:22.769 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onStop DEVTEAM
11-20 08:32:27.122 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onDestroy DEVTEAM
After hitting back button:
11-20 08:32:27.299 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onCreate null
11-20 08:32:27.300 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onStart null
11-20 08:32:27.315 4906-4906/giphouse.nl.proprapp E/GroupOverviewActivity: onResume null
Note that onRestoreInstanceState is not called. In onCreate, the savedInstanceState is null.
For completeness, the relevant parts of the associated AndroidManifest:
<activity
android:name=".ui.group.overview.GroupOverviewActivity"
android:label="@string/title_activity_group_tabbed"
android:parentActivityName=".ui.group.GroupListActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="giphouse.nl.proprapp.ui.group.GroupListActivity" />
</activity>
<activity android:name=".ui.group.GroupMembersActivity"
android:label="@string/item_title_members"
android:parentActivityName=".ui.group.overview.GroupOverviewActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="giphouse.nl.proprapp.ui.group.overview.GroupOverviewActivity" />
</activity>
SOLUTION
The solution (found by following kind pointers from realharry) was to manuallt override the onOptionsItemSelected, because the default implementation apparantly creates a new activity. (I found it here Up Button Calls OnDestroy of Parent Activity).