向上按钮调用父活动的销毁 [英] Up Button Calls OnDestroy of Parent Activity

查看:248
本文介绍了向上按钮调用父活动的销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在顶部,我想澄清一下:我正在努力使用的按钮不是back按钮.我指的是应用顶部的ActionBar/工具栏中的up/home按钮,而不是底部的Android按钮.有一些类似的帖子自然,但它们针对的是后退"按钮,而不是向上"按钮.

Right off the top, I want to clarify something: The button that I am struggling with is NOT the back button. I am referring to the up/home button in the ActionBar / Toolbar at the top of the app, not the Android button at the bottom. There are a few posts of a similar nature, but they address the back button, not the up button.

在这种情况下:我有一个具有ListView片段的ActivityA.当用户单击列表视图项时,它会启动活动B.这很典型.活动A在工具栏中具有EditText字段,该字段允许用户输入搜索参数.如果用户从活动B中单击up/home按钮,则我将成功返回活动A.但是,我希望活动A在EditText字段中显示与他们离开时相同的文本.如果用户单击back按钮,则将还原此文本.但是如果使用up/home按钮进行导航,则EditText字段为空.

Here is the situation: I have an Activity A that has a ListView fragment. When the user clicks on a list view item, it launches Activity B. Pretty typical. Activity A has an EditText field in the toolbar that allows the user to enter a search parameter. If the user hits the up/home button from Activity B, I return successfully to Activity A. However, I want Activity A to show the same text in the EditText field that was there when they left it. If the user hits the back button, this text is restored. But if they navigate with the up/home button, the EditText field is empty.

使用一些日志语句,我可以看到,当从活动A中点击列表项时,会同时调用onSaveInstanceStateonStop(但此时未调用onDestroy.)在活动B中,当点击up/home按钮,立即调用来自活动A的onDestroy,然后紧接着是onCreate,依此类推.但是,捆绑包savedInstanceState为空,大概是因为onDestroy只是叫.

Using some log statements, I can see that when a list item is tapped from activity A, onSaveInstanceState and onStop are both called (but onDestroy is NOT called at that point.) From activity B, when the up/home button is tapped, onDestroy from activity A is immediately called, followed by onCreate, etc. However, the bundle savedInstanceState is null, presumably since onDestroy was just called.

为什么返回到活动A时会调用onDestroy?这对我来说毫无意义.这是清单中的内容:

Why is onDestroy called when returning to Activity A? This makes no sense to me. Here is what I have in the manifest:

<activity
  android:name=".Activity.ActivityA"
  android:label="@string/app_name"
  android:parentActivityName=".Activity.ParentActivity"
  android:theme="@style/AppTheme"
  android:launchMode="singleTop"
  android:windowSoftInputMode="stateVisible" />

<activity
  android:name=".Activity.ActivityB"
  android:label="@string/app_name"
  android:parentActivityName=".Activity.ActivityA"
  android:theme="@style/AppTheme" />

以下是活动A中的相关方法:

Here are the relevant methods in Activity A:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        actionBar = getSupportActionBar();

        if (actionBar != null)
            initializeActionBar();

        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Log.d(TAG, "on create");

        if (savedInstanceState != null) {
            Log.d(TAG, "saved instance state not null");
            if (savedInstanceState.getString("search_text") != null)
                etSearch.setText(savedInstanceState.getString("search_text"));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("search_text", etSearch.getText().toString());
        Log.d(TAG, "on Save instance state");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        Log.d(TAG, "on restore instance state");

        if (savedInstanceState != null) {
            if (savedInstanceState.getString("search_text") != null)
              etSearch.setText(savedInstanceState.getString("search_text"));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "on resume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "on stop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "on destroy");
    }

private void initializeActionBar() {

    actionBar.setCustomView(R.layout.actionbar_with_edittext);

    etSearch = (EditText) actionBar.getCustomView().findViewById(R.id.actionbar_searchfield);
    etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if(event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                initiateNewSearch();
                etSearch.clearFocus();

            }

            return false;
        }
    });

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT);
    etSearch.requestFocus();
}

我认为活动B中的任何代码在这里都不相关.

I don't think that any of the code in Activity B is relevant here.

这是用户在活动A中点击列表视图项时我的控制台输出:

This is my console output when a user taps on a listview item in Activity A:

保存实例状态为
停止

on Save instance state
on stop

然后这是用户在活动B中点击up/home按钮时生成的:

And then this is what is generated when the user taps on the up/home button from activity B:

破坏时
创造时
恢复时

on destroy
on create
on resume

如果还有其他帮助的地方,请告诉我.感谢您的任何建议!

If there is anything else that may be of help, please let me know. Thanks for any advice!

推荐答案

我不知道为什么默认的向上按钮实现会创建一个新的活动,但是对我来说可行的解决方案是覆盖onOptionsItemSelected:

I don't know why default up button implementation creates a new activity but a working solution for me is to override onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id== android.R.id.home ){
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

此解决方案也对我有用:

Also this solution works for me:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = NavUtils.getParentActivityIntent(this);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                NavUtils.navigateUpTo(this, intent);
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

这篇关于向上按钮调用父活动的销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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