Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass data from One activity to another without using intents?

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

like image 271
zyonneo Avatar asked Nov 22 '12 15:11

zyonneo


1 Answers

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

First Activity

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;
   }
 }

Second Activity:

 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.

like image 169
krushnakant Avatar answered Nov 14 '22 22:11

krushnakant