Reputation: 4371
I'm doing some android code today and now the problem is I need to get the variables passed. The scene is that I need to login first to facebook then fetch my information. afterwards here comes a splash screen where it will display my name, etc info using the intent. afterwards the main screen will now appear and all of the data from the splash screen will now be passed to the main screen using also an intent but the problem is that this main screen is just a container for my tabViews wherein I can navigate anytime from one page to another and I think there is no need for me to use the intent again in this part. Now I need to get all the passed data from which is passed to the main screen down to one of my tab and display it on a TextView.
Upvotes: 0
Views: 1665
Reputation: 3485
You may use SharedPreferences to pass value like this:
//for write id one Activity
SharedPreferences spre = context.getSharedPreferences("Your prefName",
Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = spre.edit();
prefEditor.putString("key", value);
prefEditor.commit();
// for get id second Activity
SharedPreferences spre = context.getSharedPreferences("Your prefName",
Context.MODE_PRIVATE);
String mystring= spre.getString("key","");
Upvotes: 1
Reputation: 4489
Simplest way to do this is, you can use a data Class with static variables to use it everywhere. Something like this.
public class User {
public static String name;
public static String email;
.....
}
and then, after receiving data from Facebook, you can fill this and can access it everywhere.
Another ways to perform required task are SharedPreferences and Application Object of your class. You can read about them here and here.
Upvotes: 1
Reputation: 392
suppose for first class is A and another class is B and you want to send data form A to B then i.e you want to send user_id from class A to B then in your class A declare user_id as public string. in class B access your user_id as id = A.user_id.
Hope this help you out.
Upvotes: 0
Reputation: 8816
You can use the android.app.Application
class for this purpose.
Steps to creating & using the Application class:
Create your Application class. An example is shown below:
public class ApplicationController extends Application {
//Application wide instance variables
//Preferable to expose them via getter/setter methods
@Override
public void onCreate() {
super.onCreate();
//Do Application initialization over here
}
//Appplication wide methods
//Getter/Setter methods
}
Specify your Application class in AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".ApplicationController">
Get the instance of the Application Class instance and call various methods:
ApplicationController AC = (ApplicationController)getApplicationContext();
Hope this helps.
Upvotes: 3
Reputation: 22291
Define Variable as Public Static or another option is store variable's value into Shared Preferences and retrieve value of preferences in next activity and refer below links for more information.
Upvotes: 1
Reputation: 109247
Use public static variables in Main Screen for this. Or you can use Application class also for this purpose. other option is SharedPreferences.
For Application class global variables look at this SO question How to declare global variables in Android?
Upvotes: 1