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

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(...)

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.

regarding passing data through intent

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.

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.

Basic Android code flow

Lets say you have one main activity called "main", and other sub-activities that do some work and then return some data. Each time main gets called it starts up the sub-activities one at a time.
Main:
onCreate...{
Intent i1 = new Intent("com.bla.bla.activity1");
startActivity(i1);
//Get bundles from activity1 and save some variables..
Intent i2 = new Intent("com.bla.bla.activity2");
startActivity(i2);
//Get bundles from activity2 and save some variables..
Intent i3 = new Intent("com.bla.bla.activity3");
startActivity(i2);
//Get bundles from activity1 and save some variables..
}
If I understand correctly, activity1 will be created at first, and then destroyed. Will the code resume and start activity2 or will it just start activity1 again because the main activity was paused and then resumed (assuming we don't add an onResume/onPause etc..)?
Edit:
Lets say these sub-activities return one string each, and each string should be saved in one individual column of an SQL entry at the end of the main activity. The main activity should collect the three strings, save it as en entry and the destroy itself.
For this to work, the main activity has to resume for example at intent "i2" after intent "i1" has done some work and returned the string.
How is this result best achieved?
Like Alex said, you wouldn't want to do that. You want to use the different Intents to call different Activities to create a flow to your program. Main might lead to Activity 1, Activity 1 leads to 2, 3, or 4. If you have several activities, you can use a switch statement to determine which activity you want to launch, based on conditions that are, or are not, met.
Hope this helps.
EDIT Answer:
If you want to save variables from different Activities, the you would want to start the second Activity from the first, the third, from the second, and so on. To create the data for the database, you can simply create an object, and then pass that object from Activity to Activity, using the Parcelable interface. Within each of your Activities you would want to use methods to save the returned data to the database, as you see fit.

Categories

Resources