android - 将 bool Intent 从 onBackPressed() 发送到 onStart()

标签 android android-intent

当我按下 Map Activity 中的后退按钮时,我试图将 Map Activity 的 Intent 发送到 MainActivity。应该在 MainActivityonStart() 中接收 Intent ,但我只是在那里获取 extras NULL . Map Activity 中的变量 serviceStatus 在用户单击操作栏中的按钮时设置。我想在 MainActivity 的操作栏中设置服务的状态。

我已经尝试从 Map Activity 中的 finish() 发送 Intent ,但我也得到了额外的 null。

我该如何解决?

map Activity :

public class Map extends ActionBarActivity{
 boolean serviceStatus;

    @Override
    public void onBackPressed() {
        super.onBackPressed();      
        Intent serviceStatusIntent = new Intent(Map.this, MainActivity.class);
         // I have debugged it and serviceStatus is false here also the intent exists.
        serviceStatusIntent.putExtra("ServiceStatusMapExtras", serviceStatus);
        startActivity(serviceStatusIntent);
    }

主要 Activity :

public class MainActivity extends ActionBarActivity {
    @Override
boolean serviceStatus = true;
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();                
        Bundle extras  = getIntent().getExtras();
        //I am receiving null here.     
        if(extras != null){
            serviceStatus = extras.getBoolean("ServiceStatusMapExtras");

        }

最佳答案

移除 super.onBackPressed();

修复 MainActivity:

public class MainActivity extends ActionBarActivity {
        @Override
      boolean serviceStatus = true;
        protected void onStart() {
            super.onStart();                
            serviceStatus =  getIntent().getBooleanExtra("ServiceStatusMapExtras", false);

       }
}

关于android - 将 bool Intent 从 onBackPressed() 发送到 onStart(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32361684/

相关文章:

android - Socket 连接建立时发出蜂鸣声通知

android - 如何每30分钟后发出警报

android - BadParcelableException - BroadcastReceiver Intent 崩溃

php - Android向数据库发送纬度和经度时出现空指针异常

java - Android 应用程序 - 带有相机应用程序的弹出菜单

android - 将用户发送到 Intent 设置后如何返回我的应用程序

android - 获取值(value) Firebase

android - 如何使用 Android SyncAdapter?

android - Kotlin 从工厂方法返回相同的对象

java - 动画 hasStarted() 或 hasEnded() 不起作用