Android的 - 早在标题栏按钮早在、标题栏、按钮、Android

2023-09-03 20:34:47 作者:姐&霸道范儿

在许多应用程序(日历,驱动器,Play商店),当你点击一个按钮,然后输入一个新的活动,在标题栏的图标会变成一个返回按钮,但是对于应用程序,我想提出,它不会做那。如何使该图标带你回到previous屏?

In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, it doesn't do that. How do I make that icon take you back to the previous screen?

推荐答案

有两个简单的步骤,以在标题栏中创建一个后退按钮:

There are two simple steps to creating a back button in the title bar:

首先,使用下面的code。在你想有一个返回按钮,其标题栏中的活动使应用程序的图标点击:

First, make the application icon clickable using the following code in the activity whose title bar you want to have a back button in:

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

在添加了上述code,你会看到一个回头箭出现在应用程序图标的左侧。

After you have added the above code you will see a back arrow appear to the left of the application icon.

二,你在上面做了之后,你仍然必须创建code,将采取click事件的优势。这样做,要知道,当你真正点击应用程序图标上onOptionsItemSelected方法被调用。所以要回到previous活动的方法添加到您的活动,并把意图code在它会返回到previous活动。例如,让我们说,这种做法你想回去叫MyActivity。要回到它写的方法如下:

Second, after you have done the above you still have to create code that will take advantage of the click event. To do so be aware that when you actually click on the application icon an onOptionsItemSelected method is called. So to go back to the previous activity add that method to your activity and put Intent code in it that will return you to the previous activity. For example let's say the activity you are trying to go back to is called MyActivity. To go back to it write the method as follows:

public boolean onOptionsItemSelected(MenuItem item){
    Intent myIntent = new Intent(getApplicationContext(), MyActivity.class);
    startActivityForResult(myIntent, 0);
    return true;

}

这就是它!

That's it!

(在Android开发者API方面,它建议的清单和添加的东西,如机器人插科打诨:。parentActivityName但是,这似乎并没有为我工作上面的更简单,更可靠的)

(In the Android developers API it recommends messing around with the manifest and adding stuff like android:parentActivityName. But that doesn't seem to work for me. The above is simpler and more reliable.)