我有一个包含两个活动和两个不同工具栏的应用。我想回到第二个主要活动。我在manifest.xml中添加了父活动,在第二个活动中添加了监听器,但没有,后退箭头在那里,但它不起作用。
这里是代码:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_fw_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:logo="@drawable/ic_action_fw_light"
android:name=".RxApplication">
<activity android:name=".HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".SearchActivity"
android:parentActivityName=".HomeActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity android:name=".WebPageActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
</application>
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home"
android:visibility="visible"/>
<include
android:id="@+id/toolbar_search"
layout="@layout/toolbar_search"
android:visibility="gone"/>
MainActivity的onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Calling onCreate for HomeActivity");
super.onCreate(savedInstanceState);
setContentView(getLayoutRedIs());
Toolbar tb = (Toolbar) findViewById(R.id.toolbar_home);
setSupportActionBar(tb);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
和第二项活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Calling onCreate for SearchActivity");
super.onCreate(savedInstanceState);
setContentView(getLayoutRedIs());
Toolbar tb = (Toolbar) findViewById(R.id.toolbar_home);
tb.setVisibility(View.GONE);
tb = (Toolbar) findViewById(R.id.toolbar_search);
tb.setVisibility(View.VISIBLE);
setSupportActionBar(tb);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}