regarding passing data through intent - android

I have an app with 3 Activities.
If the app runs for the first time, the flow is like this
Activity 1 ---> Activity 2 ---> Acitvity3
If its not the first Run, then it skip sholud Activity2
Activity 1 ---> Acitvity3
Activity 3 expects some data from Activity2.
Bundle b = getIntent().getExtras();
Integer mInt= b.getInt(filename);
Acitvity 1 doesnt send this data, which forces the app to crash if Activity 3 is started from Acitvity1 (i.e. for not first run). Is there any way by which I can skip checking for getExtras in Activity3 if its called by Activity1??

Whenever starting Activity3 from Activity1 ot Activity2 pass the one string variable like this..
if from ActivityA
intent.putExtra("from", "ActivityA");
if from ActivityB
intent.putExtra("from", "ActivityB");
and in the Activity3 get the value and check if it its from Activity2 perform the operation..
String stringExtra = getIntent().getStringExtra("from");
if (stringExtra.equals("ActivityA")) {
}else {
}

There is no way in Android to get the Activity which called your current Activity. Think about it, the calling "activity" might be the home page.
Instead, you could try passing another variable in the extras saying what activity is calling the new activity (a simple string, or even a Class object) and then according to which activity is being called, you could decide what to do with a simple "if" statement.
Hope this helps :)

Add a boolean extra as a flag to determine which activity started Activity3. Before trying to get mInt from extras, read the boolean first, which you would set to false in Activity 1's Intent launching Activity 3, and set to true in the Intent in Activity2.

Related

How to start an app with Activity1 or Activity2?

I don't know how to implement this situation.
I have an app, during the start of the app, I need to calculate something and then, based on the results, start or Activity1 or Activity2.
How can I achieve this goal?
If I put the calculations in Activity1, and the calculations wants to start Activity2, however Activity1 starts Activity2 and then suddenly finish(), but the screen flickers because there is an activity that starts and ends in a few msecs. This is not very smart.
you should use a splash activity , and put your calculations there , and then based on the result you start the activity you want to
Many ways you can do that. Let me share you one example given as follows
Create New activity ( StartActivity ) which is your root activity.
Within this activity initialize some object like ( App Config, fabric, push notification, share preference data etc.)
Here you can calculate your necessary data and pass using bundle
Create Intent Object for translation to Activity1.class
if(BuildConfig.DEV_ENV && CounterPreference.getInstance().isFirstStart()) {
Intent intent = new Intent(this, Activity1.class);
startActivityForResult(intent, DEBUG_OPTIONS_ACTIVITY);
} else {
StartupManager.getInstance().start(StartActivity.this);
Logger.d(TAG, "Application Started.....*************************");
}
Add your calculation in Application class onCreate() method.And Add your activity decision in Application class.

Proper data transfer between activities

I have few questions about passing data to another activity. Let's say I have three activities I called them
first activity
second activity
third activity
When app is enabled by user, the user is in the first activity. To go from the first activity to the third activity user must also go through the second activity. The workflow
first activity (user push button) ->
second activity (user push button) ->
third activity
Let's assume I need data from the first activity in the third activity, so I need to pass my HashMap with my data from first activity to the second activity and then to the third activity?, is there any way to pass data directly from first activity to third activity but like I said, user has to go also through second activity.
I'm doing this in that way...
Intent secondActivity = new Intent(this, secondActivity.class);
secondActivity.putExtra("myHashMap", dataHashMap);
this.startActivity(secondActivity);
then in the second activity I have this part of code
secondActivity = getIntent();
dataHashMap = (HashMap<String,
String>)secondActivity.getSerializableExtra("myHashMap");
and then I'm repeating this upper two steps for the third activity. Is this correct way? because looks a little bit confusing and time consuming. Also there is another problem, now if I am in the third activity and I want to go back to the second or first activity I have always pass some HashMap because activities expects this type of data. Is there a way to sometimes return to a previous activity but no longer pass any data (HashMap in my case) if it is not needed?.
secondActivity.putExtra("myHashMap", dataHashMap); //- I would like to sometimes skip this step
I tried to this code below to handle with this but it does not work, my app keep stopping/crashing because second activity expects hashMap data from the third activity.
secondActivity = getIntent();
if(secondActivity != null){
dataHashMap = (HashMap<String,
String>)secondActivity.getSerializableExtra("myHashMap");
}
Thanks for answers, regards.
You can use a variable that is placed inside Application class instance. This variable is shared for all activities and other elements of the application.
You have several options. Firstly, you said in a comment that you only need one element. In that case you can just pass the one element as an "extra" in the Intent, instead of passing the entire HashMap.
Secondly, I don't understand why you return from the third Activity to the second Activity by calling startActivity(). You should just call finish() and that will return you to the second Activity.
If you need to return some data from the third Activity to the second Activity, you should start third Activity using startActivityForResult() and set the data to return in third Activity using setResult() and the data will be returned to second Activity in onActivityResult().
Another option is to do it exactly as you have done it, but also copy the hashMap into the Intent as an "extra" each time you call startActivity(). If you only have a handfull of entries in the hashMap it won't make any noticeable difference in performance.

How to identify which Activity was 'opened' in onResume

In my main activity I want to override the onResume method so it acts differently, depending on the activity, which was opened before.
For example: if the user creates a new Item for the listView (in the addNewItem activity), I need to reload the database to display the newItem.
But if he just switches into an activity, which doesn't change anything with the objects displayed in the main activity, the database shouldn't be reloaded and the GUI shouldn't be build again.
Therefor I need to know which activity was 'opened' before.
I hope you understand my problem.
A dirty way is to extend your application class and set an attribute lastActivity that you would set in every onResume/OnPause methods of every activities in your app. This way, when your main activity on resume is called, just read this lastActivity field to know where you come from. But I think it's the dirty way to do it :D
You can send something through in your intent when you call the Activity.
Intent myIntent = new Intent(Activity1.this,Activity2.class);
Bundle myData = new Bundle();
myData.putString("previousActivity", "Activity1");
myIntent.putExtras(myData);
startActivity(myIntent);
Then in the new Activity, you access this and compare the result:
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
String str1 = myBundle.getString("previousActivity");
if string.equals("Activity1"){
// do code changes for activity 1 here
}
There are slightly more refined ways of doing this (i.e passing through an int variable which corresponds to a particular Activity) but this is the most basic way.
The right way to do it, is to start every activity with startActivityForResult(intent, resultCode).
When an activity exits, it can call setResult(int) to return data back to its parent.
Link to Developers Resource

How to get back to the first activity when opening multiple activities

I have an app which have HomeActivity and 4 activities A,B,C,D.
I want when clicking on button start_activity_A_btn in HomeActivity to star activity A, and A starts B, B starts C, C starts D, then done button which takes me to HomeActivity.
NOTICE : in every activity (A,B,C,D) I have some data to save and get back to the HomeActivity after pressing done button.
What you need to do use the following flag in your intent (please check the link, it explains a similar situation to the one you are facing): FLAG_ACTIVITY_REORDER_TO_FRONT. So, in your activity D, in the onClickListener for the done button, here's the code you'd have to use:
Intent intent = new Intent(this, ActivityAname.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("data", dataYouReceiveFromABCD);
startActivity(intent);
This will get your A Activity to resume.
Now, regarding the "data" you will just have to keep accumulating this data in a String using some separator if that's possible (since you haven't told us what this data is exactly), so if it was a username and a password you could separate the two using a random combination of characters that will possibly never occur ("246#$^") and then just keep creating a string that you keep building in A, B, C and D and then finally in D you put that String as an extra in the intent (check the code I've posted above). If it's some other sort of data then you could perhaps serialize it if that helps. However, if you do use a String with a predetermined separator then all you will have to do is in Activity A you will have to use the following code in the onResume() method of Activity A.
if(this.getIntent().getExtras().getString("data") != null)
{
String data = this.getIntent().getExtras().getString("data");
//do some stuff here with that data
}
If you need data returning an Activity, you should use startActivityForResult to start ABCD. This works very much like your HomeActivity is opening up some dialog, and once the Activity finished (by pressing done or cancel, depends), you get onActivityResult in your HomeActivity.

In Android, while switching between the screens the GPS data is lost

I have three activities, Activity1, Activity2, and Activity3.. Activity1 is the main activity from which am switching to other two activities. While I switch to Activity2 from main activity it starts properly, when I switch to Activity3 from main activity and come back to the Activity2 all previous data will be lost and it starts from starting.
Is there any other way to switch to the activities other than using the startActivity() method.
You can store the data in static variable in another class and can call these whenever required.. i think it will work
Hmm, you seem to need to pass information between Activities.
You should use a Bundle to pass information from Activities 1 to 3 to 2,
Or use startActivityForResult(Intent, int) if you're going from Activities 1 to 3 to 1 to 2.
Try this, hope it helps.
Sender.java:
Intent i = new Intent(this, Receiver.class);
i.putExtra("object_name", object);
startActivity(i);
Receiver.java:
Bundle extras = getIntent().getExtras();
Type _variable = extras.getTypeExtra("object_name");
Do note that getTypeExtra(...) requires you to specify the type of information you are about to retrieve.
ex. getStringExtra(...), getFloatExtra(...)

Categories

Resources