【问题标题】:Unable to go back to parent activity with toolbar back navigation in Android无法使用 Android 中的工具栏返回导航返回父活动
【发布时间】:2020-07-06 06:42:58
【问题描述】:

背景

我有两个活动,分别是MainActivityAddPersion。我还定义了一个 java 类,即BaseActivity,它实现了工具栏和菜单项。

我想在 Intent 的帮助下从 MainActivity 移动到 AddPerson 活动,然后通过单击返回导航返回到 MainActivity。我通过以下视频进行了尝试: How to add Android Back Button

问题

当我在移动到 AddPerson 后单击返回导航按钮时,没有任何反应,即我没有被移动回 MainActivity

代码

基础活动

public class BaseActivity extends AppCompatActivity {

     private Context context; // tried Activity context too with no luck

     // code for onCreate here 
     // code for onCreateOptionsMenu here

     @Override
     public boolean onOptionsItemSelected(MenuItem item){    

         switch (getItemId()){
            case R.id.share: 
                 // code for share here
                 break;                
            case R.id.addPerson: 
                 startActivity(new Intent(getContext(), AddPerson.class));
         }
         return true;
    }

    public void initToolbar(boolean isChild){

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(isChild);
    }

    public void setContext(Context context){ this.context = context; }
    public Context getContext(){  return context; }
}

MainActivity

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initToolbar(false);
        setContext(this);
    }
}

加人

public class AddPerson extends BaseActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_person);
        initToolbar(true);
     }
}

AndroidManifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".AddPerson"
        android:parentActivityName=".MainActivity">
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我知道问题出在上下文中,但我不知道如何解决。

【问题讨论】:

    标签: android android-activity android-context


    【解决方案1】:

    在 AddPerson Activity 中,您应该覆盖 onOptionsItemSelected() 使用switch case,为androi.R.id.home添加一个case,并申请onBackPressed()

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

    【讨论】:

    • 我不想定义home 菜单项。只想更新活动的上下文。
    【解决方案2】:
        public void initToolbar(Context from,Context to,boolean isChild){
        Toolbar toolbar = findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
       getSupportActionBar().setDisplayHomeAsUpEnabled(true);
       toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new 
    
    
      Intent(from,to)
         .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    
    }
    });
    }
    

    如果您在 AddPerson 活动中并想转到 Main 活动,您可以像这样调用函数:

    initToolbar(AddPerson.this,MainActivity.class,true);
    

    您可以根据需要更改参数。

    【讨论】:

    • 显示错误AddPerson is not an enclosing class。我不想使用自定义事件监听器。
    • 在 AddPerson 本身中执行;
    • 我在BaseActivity 中定义了initToolbar 而不是AddPerson。在AddPerson 我只是打电话给它。
    • 所以你需要在每个类中添加上下文作为参数,我将在我的另一个答案中告诉你如何
    • 显示错误:cannot resolve constructor 'Intent(android.content.Context, android.content.Context)。我不认为这是正确的方法。除此之外,正如我所说,我不想像我那样使用事件监听器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多