如何通过从一个活动到另一个数据,而无需使用意图? [英] How to Pass data from One activity to another without using intents?

查看:113
本文介绍了如何通过从一个活动到另一个数据,而无需使用意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用挂起intent..So如果我用另一种意图,并启动它的意图去一个新的activty..I要避免that..So不使用意图,我想从一个活动来传递数据另一种可能吗?

I am already using a pending intent..So if i use another intent and start that intent it goes to a new activty..I want to avoid that..So without using intents i want to pass data from one activity to another possible??

推荐答案

您可以使用静态方法来获取数据从一个活动到另一个activity.Below为样本code

You can use static Method to get data from one activity to another activity.Below is sample code

第一项活动

public class First extends Activity {

 static First INSTANCE;
 String data="FirstActivity"; 

 @Override
  public void onCreate(Bundle savedInstanceState) {

    INSTANCE=this; 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   }

 public static First getActivityInstance()
   {
     return INSTANCE;
   }

 public String getData()
   {
     return this.data;
   }
 }

第二项活动:

 public class Second extends Activity {

 String data; 

 @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);
    data=First.getActivityInstance().getData();  
    Toast.makeText(Second.this,"Data from first activity is"+data, 1).show();
   }
}

希望它可以帮助你。

Hope it helps you.

这篇关于如何通过从一个活动到另一个数据,而无需使用意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆