简体   繁体   English

从当前活动返回时,显示上一个活动的按钮

[英]Show a button on a previous activity when going back from current activity

I want to visible a edit button on my previous activity by going back using back button. 我想通过使用后退按钮返回来查看上一个活动中的编辑按钮。 But when I am using 但是当我使用

        @Override
            public void onClick(View arg0) {
                // finish the current activity
                finish();

            }

on my current activity, it is going back to previous activity, but edit button doesn't become visible. 在我当前的活动中,它可以返回到上一个活动,但是“编辑”按钮不可见。

If I am using onResume on previous activity 如果我在上一个活动中使用onResume

@Override
    protected void onResume() {
        super.onResume();
        btnEdit.setVisibility(View.VISIBLE);

    }

then it's always visible, no matter if the activity is resuming or created for the first time. 那么无论活动是第一次恢复还是首次创建,它总是可见的。

I am new in Android development, please help me to solve this problem. 我是Android开发的新手,请帮助我解决此问题。

您可以创建一个布尔变量,完成后可以将其设置为true,并在onResume方法中对此布尔值进行检查

Override onRestart() method 重写onRestart()方法

@Override
protected void onRestart () {
    super.onResume();
    btnEdit.setVisibility(View.VISIBLE);

}

Or start Activity with 或开始活动

startActivityForResult (Intent intent, int requestCode)

method and override 方法和覆盖

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
    super.onActivityResult (requestCode,resultCode,data);
    btnEdit.setVisibility(View.VISIBLE);

}

Ok, 好,

When you start the Activity, instead of startActivity(intent) use startActivityForResult(intent, 1989) (The 1989 can be whatever int you want). 当您启动Activity时,请使用startActivityForResult(intent, 1989)而不是startActivity(intent) startActivityForResult(intent, 1989) (1989可以是您想要的任何int值)。

When you return from the new Activity, before calling finish() do the following: 从新的Activity返回时,在调用finish()之前,请执行以下操作:

        Intent returnIntent = new Intent();
        returnIntent.putExtra("resultBool", true);

        //Null checks, not strictly neccescary 

            if (getParent() == null)
            {
                setResult(Activity.RESULT_OK, returnIntent);
            }
            else
            {
                getParent().setResult(Activity.RESULT_OK, returnIntent);
            }

    finish();

Then in your first Activity override onActivityResult() like so: 然后在您的第一个Activity中覆盖onActivityResult()如下所示:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            // The int you initially used
            if (requestCode == 1989)
            {
                if (resultCode == RESULT_OK)
                {
                    boolean result = data.getBooleanExtra("resultBool", false)

                    if (result)
                      //Show the button now

                }
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 后退按钮不会转到活动堆栈上的上一个活动 - Back button not going to previous activity on the activity stack 返回上一个活动,并通过附加的“后退”按钮片段化 - Going back to the previous activity and fragment from an additional back button 按下返回按钮无法从当前活动转到上一个活动 - Unable to go to previous activity from current activity on back button pressed 当我单击后退按钮从当前活动 android 转到上一个活动时 - When I click back button goto previous activity from current activity android 从当前活动返回时,对之前的活动执行某些操作 - Do something on previous activity when back pressed from current activity Android活动后退按钮可返回上一个活动,但没有数据 - Android activity back button going back to previous activity but no data 在当前活动上单击按钮(不是后退按钮)时关闭当前和上一个活动 - Closing current and previous activity on click of button(not the back button) on current activity 后退按钮子视图不返回上一个活动 - Back Button Child View not going back to previous activity 回到android中的上一个活动 - Going back to the previous activity in android EmailIntent返回上一个活动 - EmailIntent going back to previous activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM