繁体   English   中英

存储一个活动的意图,但是开始另一个活动

[英]Store intent for one activity but start a different activity

我是android的新手,但有问题。 我想存储活动的意图,但是开始其他活动。 我有一个backgroundworker活动,该活动必须存储postAnnotationActivity的意图,但是必须启动InstructionsActivity。 如果我在PostAnnotationActivity中使用intentID.getStringExtra ,则输出= null 有人可以帮忙吗?

Backgroundworker的部分代码:

        Intent intent = new Intent(context, InstructionsActivity.class);
        Intent intentID = new Intent(context, PostAnnotationsActivity.class);
        intentID.putExtra(ID, id);
        context.startActivity(intent);

PostAnnotationActivity的一部分:

    Intent intentID = getIntent();
    String getID = intentID.getStringExtra(BackgroundWorker.ID);

从InstructionsActivity,我将使用startActivity(new Intent(this, ShowPaintingActivity.class)转到StartActivty,然后使用startActivity(new Intent(this, PostAnnotationsActivity.class));

修改您的代码,如下所示:

在Backgroundworker中:

 Intent intentID = new Intent(context, InstructionsActivity.class);
 intentID.putExtra("ID", id);
 context.startActivity(intentID);

在InstructionsActivity中:

 Intent intentID = getIntent();
 String getID = intentID.getStringExtra("ID");

 Intent intent = new Intent(context, ShowPaintingActivity.class);
 intent.putExtra("ID", getID);
 context.startActivity(intent);

在ShowPaintingActivity中:

 Intent intentID = getIntent();
 String getID = intentID.getStringExtra("ID");

 Intent intent = new Intent(context, PostAnnotationActivity.class);
 intent.putExtra("ID", getID);
 context.startActivity(intent);

在PostAnnotationActivity中:

Intent intentID = getIntent();
String getID = intentID.getStringExtra("ID");

另一个更好的解决方案是,如user1375469所建议的,您可以将值保存到Backgroundworker中的首选项,然后从PostAnnotation活动中检索它。

尝试将您的Intent存储为字符串,并使用共享的首选项存储/检索您的Intent。 像这样

    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();

    editor.putString("StoredIntent", "String you want to store here");
    editor.commit();

您正在获得输出null,因为您没有启动Activity,即

 context.startActivity(intentID);  

当您将数据传递到其中时。
并且您想转到我的InstuctionsActivity,但已经存储了来自BackgroundWorker的一些数据用于PostAnnotationActivity。...
因此,您必须为此目的使用SharedPreference。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM