Passing value of a variable from 1st activty to 3rd activity - android

In passing the value of a variable from 1st activity to 3rd activity should I use intent.putextra or should I make the variable a global variable so that I could use it in any activity.

You have several options :
implement your own Application class, and make this variable an attribute of the class
save and get this variable in the preferences

The best way to do that is to use intent.
Nevertheless, you sometimes want a more persistant variable, or store a more complexe object. In these cases, you can use a static variable, in a singleton class for instance.

This is definitely your decision but I suppose the user can go to Activity 2 and press back, and thus canceling the need for this variable.
The best option would be to send the value as an extra to Activity 2. It can then send it to Activity 3 if needed.
REMINDER
Do not use static variables for Static Variables are WRONG Almost Always

you can pass the sesssion id to the signout activity in the intent ur using to start the activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)
Note: Make the session ID available to every activity where you want to allow the user to signout. Otherwise, you could store it in the Application object, but then you'll have to manage the state of the session (checking if it's valid before using it,...)

Related

Android passing values to activities that have not yet started

I am running into an issue with passing values to activities. I have a Title activity that launches Generating activity. Generating activity launches Play activity. Suppose I want to pass some value such as a boolean to Play activity from Title activity.
Would I have to pass it first to Generating activity and then make Generating activity pass the value to Play? Or can I pass it via putExtra() bypassing Generating activity even if Title activity does not launch Play activity directly?
An intent is a message containing data that will launch an activity. The data is only available to the target activity.
So to answer your questions:
No, you cannot pass intent data from TileActivity to PlayActivity. You must pass it to GeneratingActivity, who in turn would then pass it to PlayActivity.
There are alternatives to using intents such as:
Static/Global data - Use this with care. Because of how Android manages your process, it can be dangerous to use this approach.
SharedPreferences - This is a mechanism for persisting data. Your first activity could save the data in SharedPreferences, and the third activity could read it from SharePreferences. Because of the dangers with approach #1 above, many people take this approach in Android.
You can create a public class with a static variable :
public class Global {
public static boolean play = false;
}
You just need to call Global.play = true in your Title Activity and check the value of Global.play in your Play Activity (or other Activities based on your need)

Passing data through multiple activities in android

I have application in which I have three activities. First and second activity have listview. I need selected values from the first and second activity in order to display some data in the third activity. Which is the best way to do this? Is it better to pass Intent from first to second activity, then from second to third activity or to use SharedPrefenreces?
This depends on what you need. If you need the data to be persistent in the event that your app is destroyed, then you will probably want to save the data in SharedPreferences (will also depend on what type of data since SharedPrefs aren't limited to what they can store) or a DB.
If you aren't worried about it being persistent then passing the data through Intent.putExtra() is fine. You can also create a Bundle and pass the Bundle through the Intent and add the data as you go.
You also have the option of creating a separate class which implements Serializable. Then you can create and pass the class Object through your Intents. Here is a post about doing that
Yes this can be achieved in many ways using:
Intents
Shared Preferences
Sqlite
Sqlite: You can use Sqlite to store data if the data is huge in first and second activity and retrieve the data in the third
activity (But if data is less this is not a feasible solution)
Shared Preferences: Here the data you have saved in the shared-Preferences is done in a xml file, so even if the app is
stopped the data is retained. This is helpful to save the password
and login details etc
Intents: My opinion Passing data with intents is much better option in your case because size of the data passed between your
first, second to third activity is less.
Finalizing :: Just put the data in bundle in first activity. Next get the data(Bundle) in second activity and add the data in second activity in addition to the data in the bundle received from first activity and pass that final bundle from second activity to the third activity.
In my opinion, it would be easiest to create an ArrayManager class, in which your variables are static. This way you can create methods to select and resize your arrays in the ArrayManager class. The only case in which this is bad is if you must hold references to Views, Contexts, or Activities (this will create a memory leak). In that case, codeMagic's solution is ideal.
In your case, if you need to pass same data from first activity to second activity then third activity & your data is small then I suggest use Shared Preferences.
If data passed from first activity to second activity is not same data of second activity to third activity then simply use intent.
There is a tricks. If your second activity always need data from first activity to operate then when you want to go back third activity to second activity then you will get stuck because from third activity you can not get first activity result. In that case saving data i mean Shared Preferences or Sqlite is best.
All above those scenario you need to find your own what is best to use for your app.

What is a proper way to get data of multiple Activity in a single Activity?

I have 5 Activities in my App. Each Activity having some data. I need all previous Activity data in last Activity. All previous Activity is a kind of Form. User fill the data and move to next Activity and last Activity displays all previous Activity's data.
There are several ways to do this.
Use of Intents and save the data like putString(...):
Which isn't really nice because you have to temporarilly save the data of each Intent in the Activity and move them further with the next Intent.
Use a class for your data and pass it through the Activities per Intent:
This way you pass the instance of the class to the Activities to write/read your data. But this class needs the Parcelable / Serializable interface
Use of a static class:
You can access this class in every Activity and write/read like you want.
Use a Bundle with the Intent:
Save all your data in a Bundle and pass it to the Activities.
Think about your design and use Fragments:
Have one Activity with 5 Fragments. Every Fragment saves/reads the data from the Activity.
Best approach would be to use a Bundle.
Option 1:
If you want to add data for every activity and carryforward it to next activity then it's better to pass data with bundle.
Bundles are better in a scenario, when data is comparatively small and where you just want to pass data to next activity and don't want to store the data for future.
Option 2:
You can use Sharedprefrences. Better to use when data is to be stored for future.
Option 3:
If you have more records and you want it to be in a structured manner, and to be stored for future then Sqlitedatabase is obvious.
Above to options are in case if you want to use your data to use very frequently and it is not structured.
You can use Intent
FirstActivity
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("key_value", string);
startActivity(intent);
SecondActivity
String text = getIntent().getStringExtra("key_value");
Or if you need the data to stay longer, and you can reuse it after the application is killed, you can use SharedPrefrences
Place the data in some Activity
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("ValueOne", "SomeValue" );
editor.commit();
Retrieve data from shared preference in any Activity
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String prefString = prefs.getString("ValueOne", null);

can I use activiy one time(register activity) and switch the main launcher to different activity?

can I use activiy one time(register activity) and switch the main launcher after using to different activity?
another question if I may,
If I create parameter x in one of the activities in my application, can I use this parameter in other activities?...If yes, how I can do that?
thanks :)
You cannot dynamically change the launcher activity once it has to be only 1 activity that is defined in the manifest file.
I would recommend having something like a landing or splash activity which checks a shared preference variable, to decide which activity to launch, for example either a login activity or another activity.
You should not access a variable in an activty from another activity, you should store these in data holding classes. however if you want to do it, for a good reason, simply make it static.
You cannot adjust the manifest after running your application. What you can do is have your default launcher activity write to SharedPreferences once it has been run once. Inside of that activity check to see if that preference has been set and if it has just finish that activity and launch your new activity, the user will not see anything if you do this in the onCreate of the launcher activity.
As for passing params between activities you should use intent extras. For example to pass a string use putExtra(String key, String value), and to get that parameter inside of the new activity use getStringExtra("Key").
For global variables accessable from different activities you can also extend Application class and then access it via getApplicationContext().
1. One time activity launch
You can't change the main launcher. It's a static information. What you could do is following:
// in the beginning of onCreate
// first launch could be loaded from shared preferences
// see 2. for more
if (!firstLaunch) {
// start another activity
finish();
return;
}
2. Use data in another activity
One way is to persist the data and load it somewhere else. You will find all information you need in the Data Storage article.
If your data is primitive you could try to pass it by intent to another activity. See Using integer from one class in another Android.
If it is complex you could try to implement an own Application class and use helper methods to access global data. See Android: Accessing resources without an Activity or Context reference.
Be careful with that, please read the Avoiding Memory Leaks article then.

Android access activity1 from activity2

Hi i am new to Android application development.In my Application i have two activity Activity1 and Activity2.From activity1 i call Activity2 as Intent.I want to access activity1 from this Activity(activity2) without going to first activity is there any posible way?Pls guide me
The only thing which make sence is passing data from Activity 1 to Activity 2. To do it just pass some data through the intent:
intent.putExtra("key", "Your data here");
in second activity:
String data = getIntent().getExtra("key");
If this is not the case, then your task is wrong somewhere. When activity gone background, there is no sence to interact with it.
No, there isn't. Activity1 might even have been shut down.
If you want to pass DATA between the two activities, that's of course doable. Either by passing data on with the intent, by using http://developer.android.com/reference/android/content/SharedPreferences.html or any other storage you can access from both activities.
If from your second activity wants to change something on the previous one, then, instead of using
startActivity(...);
you should use
startActivityForResult(...);
Maybe this link can help.
So you have the scenario where you start activity B from activity A and you want to change some parameters when activity B is done ( your changes can't be propagated in real time because you can't be sure what is the state of activity A ). So the best way to implement this is to use activity result - for more info about it check Android: Capturing the return of an activity

Categories

Resources