简体   繁体   English

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

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

I am new to android and I have a problem. 我是android的新手,但有问题。 I want to store intent for an activity but start a different activity. 我想存储活动的意图,但是开始其他活动。 I have a backgroundworker activity that has to store intent for postAnnotationActivity, but has to start InstructionsActivity. 我有一个backgroundworker活动,该活动必须存储postAnnotationActivity的意图,但是必须启动InstructionsActivity。 If I use intentID.getStringExtra in PostAnnotationActivity then the output = null . 如果我在PostAnnotationActivity中使用intentID.getStringExtra ,则输出= null Can somebody help? 有人可以帮忙吗?

Part of code from Backgroundworker: Backgroundworker的部分代码:

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

part of PostAnnotationActivity: PostAnnotationActivity的一部分:

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

From InstructionsActivity I'll go to StartActivty using startActivity(new Intent(this, ShowPaintingActivity.class) and then to PostAnnotationActivity using startActivity(new Intent(this, PostAnnotationsActivity.class)); 从InstructionsActivity,我将使用startActivity(new Intent(this, ShowPaintingActivity.class)转到StartActivty,然后使用startActivity(new Intent(this, PostAnnotationsActivity.class));

Modify your code as following: 修改您的代码,如下所示:

In Backgroundworker: 在Backgroundworker中:

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

In InstructionsActivity: 在InstructionsActivity中:

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

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

In ShowPaintingActivity: 在ShowPaintingActivity中:

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

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

In PostAnnotationActivity: 在PostAnnotationActivity中:

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

And another better solution is, as user1375469 suggested, you can save value to preferences in Backgroundworker and then retrieve it from PostAnnotation Activity. 另一个更好的解决方案是,如user1375469所建议的,您可以将值保存到Backgroundworker中的首选项,然后从PostAnnotation活动中检索它。

Try storing your Intent instead as a string and use shared preferences to store/retrieve your Intent. 尝试将您的Intent存储为字符串,并使用共享的首选项存储/检索您的Intent。 Like so. 像这样

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

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

Your are getting the output null because you are not starting the Activity ie 您正在获得输出null,因为您没有启动Activity,即

 context.startActivity(intentID);  

while you are passing the data into it.. 当您将数据传递到其中时。
and you want to go to my InstuctionsActivity but already store some data from BackgroundWorker for PostAnnotationActivity.... 并且您想转到我的InstuctionsActivity,但已经存储了来自BackgroundWorker的一些数据用于PostAnnotationActivity。...
So, you have to use SharedPreference for this purpose.... 因此,您必须为此目的使用SharedPreference。

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

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