简体   繁体   English

是否可以使用意图将数据传递给其他活动,但不能启动该特定活动?

[英]Is that possible to pass data to different activity using intent but not start that particular activity?

I want to pass data to another activity but doesn't want to start the new activity. 我想将数据传递到另一个活动,但不想启动新活动。 Is it possible? 可能吗? If yes then how can i achieve it? 如果是,那我该如何实现呢?

One simple solution is that make a static variable in the other activity. 一个简单的解决方案是在另一个活动中创建一个静态变量。 You can set the data from previous activity to the new activity by simply setting this variable value. 您可以通过简单地设置此变量值来设置从先前活动到新活动的数据。

class NewActivity extends AppCompatActivity{
    public static String variable = null;

}

class OldActivity extends AppCompatActivity{

    public void xxxx(){
        NewActivity.variable = "something";
    }

}

But Note that static variable approach is bad. 但是请注意,静态变量方法是不好的。 If you really need this approach, then make it null when you don't need it anymore. 如果您确实需要此方法,则在不再需要它时将其设置为null。

If your question is in term of activity or intent I don't think so it is possible.. if you want to pass data with intent you have to start activity. 如果您的问题是关于活动或意图的,我认为这是不可能的..如果您想通过意图传递数据,则必须开始活动。

1)If you want to do some task without UI then Create service from your first activty. 1)如果您想在没有UI的情况下执行某些任务,请从第一个活动开始创建服务。

2)Or you can save that data to some static variable and then use it in you second activity. 2)或者,您可以将该数据保存到某个静态变量,然后在第二个活动中使用它。

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

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