"返回]操作栏上的按钮 - Android。如何去“回”? [英] "Back" button on Action bar - Android . How to go "back"?

查看:173
本文介绍了"返回]操作栏上的按钮 - Android。如何去“回”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片



Action Bar

我在说(第1号,图片),按钮有一个小箭头,应用程序图标和屏幕的左上角。当我们选择黑色活动模板时,它会自动定义。

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. It is automatically defined when we select the "Black activity" template.

我的应用程序有一个非常庞大的层次结构图,现在大约有25个活动。
我基本上只是显示几个教程,可以根据类别导航到。

My app has a pretty huge hierarchy graph, got about 25 activities now. I'm basically just showing a few tutorials and one can navigate to it according to the categories.

现在,Back(?)buttony我在每个屏幕上都有动作栏,我想保留它。代码显示没有错误,但是当我实际按下该按钮时,应用程序停止工作。
我想要的只是复制实际的后退按钮功能与(1号)按钮,我在图像中显示。
当我按下它,最上面的屏幕应该关闭,最后一个应该打开。只需关闭屏幕。

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. When I press it, the top most screen should close, and the last one should open. Just close the screen.

我尝试过:

@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:

我假设从 A 开始活动 B 。现在从活动 B ,您想要按动作栏上的左上角按钮返回活动 A 。只需从那里调用 this.finish() ActivityName.this.finish()

I am assuming that from activity A you are starting the activity B. Now from activity B you want to go back to activity A on pressing the top left back button on action bar. simply call this.finish() or ActivityName.this.finish() from there:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

这应该完成你当前的活动。但是,如果您有很多活动,那么您可能必须在所有活动中执行此操作。为了节省您的努力,您可以让课程让它调用它 AbstractActivity ;延伸活动。然后,您可以扩展所有其他活动类来扩展该类( AbstractActivity )。里面 AbstractActivity 你可以把上面的代码。所以现在,这段代码对所有的活动都是有效的,而且所有这些都将被实现。基本上这种事情(继承)可以随时使用有一些常见的功能将适用于您的许多课程。

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; that extends Activity. Then you can extend all your other activity classes to extend that class(AbstractActivity). Inside AbstractActivity you can put the above code. 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.

如果您收到任何错误,请发送您的 LogCat ,如果您需要进一步的帮助。
希望这可以帮助你。

If you are receiving any errors, please do post your LogCat if you require further help. Hope this helps you.

这篇关于"返回]操作栏上的按钮 - Android。如何去“回”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆