[英]“Back” button on Action bar - Android . How to go “back”?
I'm talking about the (Number 1, in the pic), button with a little arrow and the app icon and the top left side of the screen. 我说的是(图片中的数字1),带有小箭头和应用程序图标的按钮,以及屏幕的左上方。 It is automatically defined when we select the "Black activity" template.
当我们选择“黑色活动”模板时,它会自动定义。
My app has a pretty huge hierarchy graph, got about 25 activities now. 我的应用程序有一个非常庞大的层次结构图,现在大约有25个活动。 I'm basically just showing a few tutorials and one can navigate to it according to the categories.
我基本上只是显示一些教程,并且可以根据类别浏览至该教程。
Now that "Back" (?) button thingy on action bar is on every screen I have, and I do want to keep it. 现在,操作栏上的“返回”(?)按钮已经在我拥有的每个屏幕上,并且我确实希望保留它。 The code shows no error, but when I actually press that button, the app stops working.
该代码未显示任何错误,但是当我实际按下该按钮时,该应用程序停止工作。 What I want, is to just replicate the actual back button function with that (Number 1) button, that I showed in the image.
我想要的是仅使用图像中显示的那个(数字1)按钮复制实际的后退按钮功能。 When I press it, the top most screen should close, and the last one should open.
当我按它时,最上面的屏幕应该关闭,最后一个屏幕应该打开。 Just close the screen.
只需关闭屏幕即可。
What I tried : 我试过了
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
this is the the function that inflates that buggy button along with the action bar. 这是使越野车按钮以及操作栏膨胀的功能。 I tried to replace the entire code, and call "Finish" function, but that failed miserably.
我试图替换整个代码,并调用“完成”功能,但是失败了。 I was unable to find a function specifically made for that top left most button...
我找不到最左上角按钮专用的功能...
I want the top most screen on the stack(the one in the foreground) to close, when this button is touched. 我希望在触摸此按钮时关闭堆栈中最上面的屏幕(前景中的一个)。 How to do this ?
这该怎么做 ?
I think the easiest way out is follows: 我认为最简单的出路如下:
I am assuming that from activity A
you are starting the activity B
. 我假设从活动
A
开始活动B
Now from activity B
you want to go back to activity A
on pressing the top left back button on action bar. 现在,您想从活动
B
返回到活动A
,方法是按操作栏上的左上角后退按钮。 simply call this.finish()
or ActivityName.this.finish()
from there: 只需从此处调用
this.finish()
或ActivityName.this.finish()
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
This should finish your current activity. 这应该完成您当前的活动。 However if you have lot of activities, then you might have to do this in all the activities.
但是,如果您有很多活动,那么您可能必须在所有活动中都这样做。 To save yourself from that effort, you can make a class lets call it
AbstractActivity
; 为了节省您的时间,您可以创建一个类,命名为
AbstractActivity
。 that extends Activity
. 扩展了
Activity
。 Then you can extend all your other activity classes to extend that class( AbstractActivity
). 然后,您可以扩展所有其他活动类以扩展该类(
AbstractActivity
)。 Inside AbstractActivity
you can put the above code. 在
AbstractActivity
内部,您可以放置上面的代码。 So now that piece of code would be valid for all your activities and that feature would be implemented for all of them. 因此,现在这段代码将对您的所有活动均有效,并且对该功能也将对其全部实施。 Basically this sort of thing ( Inheritance )can be used any time, when there are some common features which would be applicable to your many classes.
基本上,当某些通用功能适用于您的许多类时,可以随时使用这种东西( 继承 )。
If you are receiving any errors, please do post your LogCat
if you require further help. 如果收到任何错误,请在需要进一步帮助的情况下发布
LogCat
。 Hope this helps you. 希望这对您有所帮助。
just giving basic code given by @shobhit puri... 只是给出@shobhit puri给出的基本代码...
for invoking the action bar back button..add the following code in the onCreate() method along with the onOptionsItemSelected.... 用于调用操作栏的后退按钮。在onCreate()方法中将以下代码与onOptionsItemSelected一起添加。
protected void onCreate(Bundle savedInstanceState)
{
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.