BackButton不会调用历史记录堆栈中的最后一个活动 [英] BackButton doesn't call last activity in history stack

查看:79
本文介绍了BackButton不会调用历史记录堆栈中的最后一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动出现后退按钮错误.所有活动都有此问题.当我按后退"按钮时,它将转到主屏幕,而不是堆栈中的上一个活动.我已经输入了Log消息,但在某些活动中它不会在LogCat上打印它,在此发布的此活动中它会打印日志.但是看起来有些活动没有在方法中进行.

I'm having this error with backbutton on my activities. All activities have this problem. When I press Back Button it goes to home screen, not to last activity in stack. I've putted a Log message but in some activities it doesn't print it on LogCat, in this activity posted here it prints the log. But it looks like for some activities is not going inside the method.

我的应用程序中有三个选项卡,其中一个选项卡扩展了ActivityGroup而不是Activity.但我不认为这是问题所在,我已删除了此标签,并且该标签一直在发生.

There are three tabs in my application, one of them extends ActivityGroup instead of Activity. But I don't think its the problem, I've removed this tab and it keeps happening.

所以我的问题是,如何解决它,如何使它进入上一个活动而不是进入主屏幕?

事实上,它会转到调用它的最后一个屏幕.如果在主屏幕上调用它,则转到此,如果在主屏幕上调用快捷方式和小部件,则转到此屏幕. 对于我在文档中阅读的内容,它应该转到历史记录堆栈的最后一个,这意味着该屏幕位于堆栈中,但是没有我的活动.对吧?

In fact it goes to the last screen where it was called. If I call it on the home screen it goes to this, if I call it on the main screen, where I put shortcuts and widgets it goes to this screen. And for what I've read in docs, it should go to the last in history stack, which means that this screen is on the stack, but no my activities. Right?

我已经试过放了,但是不适用于所有活动.它一直在进入主屏幕.我不知道会是什么.

I've tried to put this but doesn't work for all activities. It keeps going to home screen. I don't know what can it be.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
                finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

我也尝试了onBackPressed()方法,但是什么也没有.而且,我没有使用finish方法尝试调用新的意图,而是显式地调用了某些活动,但也没有做任何事情.

I also tried with onBackPressed() method, but nothing. And also, instead of calling finish I tried with a new intent and calling the some activity explicity, but nothing too.

由于很多,我不能将所有活动都放在这里.
但是这是一个,也许如果有什么用的话,我可以在其他人身上实现.谢谢.

I can't put all my activities here because are a lot.
But Here is one, and maybe if something works with this I can implement on others. Thanks.

public class MyActivity extends Activity {

    private ListView listview;
    private ImageView imgView;
    private Resources res;
    private Drawable transition;
    private SimpleCursorAdapter adapter;
    private DataHandlerDB handler;
    private SQLiteDatabase db;
    private OpenHelper helper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);          
        helper = new OpenHelper(this);
        db = helper.getWritableDatabase();
        setBasicContent();

    }

    @Override
    public void onStart() {

        super.onStart();
        helper = new OpenHelper(this);
        db = helper.getWritableDatabase();
        setBasicContent();

    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        DataHandlerDB.selectTopCalls(this).close();
        db.close();
        helper.close();

    }

    @Override
    public void onPause() {

        super.onPause();

    }

    @Override
    public void onStop() {

        super.onStop();
        DataHandlerDB.selectTopCalls(this).close();
        db.close();
        helper.close();

    }

    @Override
    protected void onResume() {

        super.onResume();
        setBasicContent();

    }


    public void setBasicContent() {

        listview = (ListView) findViewById(R.id.list_view);

        Cursor c = DataHandlerDB.selectTopCalls(this);      

        c.moveToFirst();

        startManagingCursor(c);     

        adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
                new String[] { DataHandlerDB.CONTACT_NAME_COL,
                        DataHandlerDB.CONTACT_NUMBER_COL,
                        DataHandlerDB.CONTACT_DURATION_COL,
                        DataHandlerDB.CONTACT_DATE_COL }, new int[] {
                        R.id.contact_name, R.id.phone_number, R.id.duration,
                        R.id.date });       

        listview.setAdapter(adapter);
    }
}


以下是扩展ActivityGroup的活动,如下所示:

tabHost.addTab(tabHost.newTabSpec("three").setIndicator("Filter Options", 
                res.getDrawable(R.drawable.filteroptionsiconfile))
                .setContent(new Intent(this, FilterOptionsGroup.class)));

FilterOptionsGroup.java

public class FilterOptionsGroup extends ActivityGroup {

    public static FilterOptionsGroup group;
    private ArrayList<View> history;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.history = new ArrayList<View>();
        group = this;

        View view = getLocalActivityManager().startActivity(
                "FilterOptions",
                new Intent(this, FilterOptions.class)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();

        replaceView(view);

    }

    public void replaceView(View v) {
        // Adds the old one to history
        history.add(v);         
        // Changes this Groups View to the new View.
        setContentView(v);
    }

}

推荐答案

如果您的所有活动都封装在选项卡宿主中,并且全部都托管在选项卡中,则它们并不是真正位于堆栈中.它们位于选项卡宿主屏幕的前景视图中,而后退按钮会将您带到上一个应用程序.

If all of your activities are encapsulated within a tab host and they are all hosted in tabs then they are not really sitting in the stack. They are sitting within the foreground view which is the tab host screen and the back button will drop you to the previous app.

这篇关于BackButton不会调用历史记录堆栈中的最后一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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