我想知道如何编写操作栏中的后退按钮以恢复到之前的活动并完成刚刚显示的活动?
About.class
public class About extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Get tracker.
Tracker t = ((AnalyticsTracker) getApplication()).getTracker(AnalyticsTracker.TrackerName.APP_TRACKER);
// Enable Display Features so you can see demographics in Google Analytics
t.enableAdvertisingIdCollection(true);
// Set screen name.
t.setScreenName("About");
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
adView ls_fragment = new adView();
fragmentTransaction.replace(android.R.id.content, ls_fragment);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
关于页面的布局。 about.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
>
<RelativeLayout
android:background="@mipmap/home"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/imageView"
android:background="@mipmap/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="300dp"
android:layout_height="250dp"
android:id="@+id/textView9"
android:gravity="center"
android:textSize="16dp"
android:text="@string/aboutText"
android:background="@drawable/text"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
<fragment
android:layout_width="match_parent"
android:layout_height="50dp"
android:name="course.labs.converjz.adView"
android:id="@+id/fragment"
tools:layout="@layout/adview"
android:layout_gravity="center|bottom"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</RelativeLayout>
创建操作栏并向其添加后退按钮的特定样式。 Styles.xml
<style name= "MenuBar" parent="@style/AppBaseTheme">
<item name = "android:homeAsUpIndicator">@mipmap/back</item>
</style>
后退按钮图像位于mipmap图标文件夹中。
答案 0 :(得分:1)
试试这个
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
答案 1 :(得分:1)
只需将getActionBar
更改为getSupportActionBar
如图所示:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);