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
Related
I am noob in Android. Suppose I have two Activity in my android app.
Activity A and B, currently I am on Activity A then on click on one button I am now Activity B. Here From Activity B I want to update some view with updated data that is in Activity A. I got updated data in Activity B so Should I use here LocalBroadCastReceiver for this or anything ?? so that When I press back then app should show me updated data in Activity A
Should we use our custom callback here to update the UI view data of Activity A from Activity B ??
No, you shouldn't use BroadcastReceiver for that. The method depends on the size of data you want to transfer from Activity B to Activity A. If it's quite small, you can start Activity B with startActivityForResult and then get data back at the onActivityResult method (in Activity B you should use setResult once you are done). If the size of data is quite big, it's better to use DB for storing it instead of keeping it in memory.
okay , there are multiple ways to do it :-
you can use a static variable to store you data that too of application level ( might not be a good approach ) check the value of vairable in onResume() and set it to the view.
you can use sharedpreferences if your data is not that large , store the data in Activity B and fetch it on onResume() method of activity A.
as #nikis has told you
if your data is too large store it in db.
I dont think broadcastreceiver fits right in your scenario !!
My app shows some content (video,pdf,img, etc ) and within every content I can start another content. What I want is to have only "one back history".
For example, if my activity history is like this:
VideoActivityIns1->PdfActivityIns1->VideoActivityIns2
I need to go back from VideoActivityIns2 to PdfActivityIns1, but one step back is should be MainActivity of my app.
How can I do this? Any help would be appreciated
Each activity has activity lifecycle methods you can override to achieve the result you need. Thus, you can either launch Activity2 onResume() on Activity1 onPause(),
http://developer.android.com/training/basics/activity-lifecycle/index.html
or, invoke ActivityManager to detect and manage the other activities.
http://developer.android.com/reference/android/app/ActivityManager.html
You can also make use of intent resolution mechanism to assign several priorities to your activities and then setup intent filters in each activity so you can start activities with a given priority in your code. You can do this either in Java or XML (though I suggest Java). Have a look at the Intent class.
I have two Activity: Activity1 and Activity2
Activity1 start Activity2 and I want to send a result from Activity2 to Activity1, but I can't use startActivityForResult() cause the lanuchmode of Activity1 is singelInstance. Are there any ways to send a callback from Activity1 to Activity2?(So far as I konw, one is send BroadCaseReceiver, the other is made a static param in Activity2)
Many thanks!
startActivityForResult not working properly with launchMode singleInstance
A similar question, it suggested using saved instance state and or saving information to a db/global storage.
onActivityResult do not fire if launch mode of activity is singleInstance
Proposes that you use a different type, replacing singleInstance with singleTask
You could extend the activity you want to launch and force its type to a different one for this scenario, leaving the original as SingleInstance.
It will not work to use broadcast to communicate between two Activities. Only one of the Activities will ever be active at a time. It does make sense to use it to communicate between an Activity and a Service, for example.
Generally you can use intent extras to pass information to the next Activity. I.e, using putExtra.
(java.lang.String, android.os.Bundle)
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.
I have an Activity which is an OpenGL view. I also have an xml layout to use for preferences. Until now, to show the preference menu, I just brought it to front by setContentView(). And the same to get back to the OpenGL view.
But is this a case where I should give the preference menu its own Activity?
I guess this would make a few things much easier. For example, the back button would just work, opposed to now where I have to code it or it will just exits the application.
And if this is a good idea, how do I pass data both ways? I have a class that store all preferences. Can I send it to the Activity and back again? Or is the best way to store the preferences in a sqlite database and then use it for passing data?
I find it easier to segregate menus and such into separate activities (unless you are using dialogs etc..) As far as storing data you can do it a number of ways:
Database
StoredPreferences
Intent extras with putExtra/Bundle
Creating an application subclass and storing preferences there
Each have their merit. 4 is pretty easy as you just have to state the application class name in your manifest then call: MyAppClass app = (MyAppClass)getApplicationContext(); and you can then use any variables in MyAppClass via app. 2 is also straightforward.
You already pointed out the main difference: history management.
You can pass data to Activity via Intents putExtra()/getExtra():
Create an Intend and add custom data via Intent.putExtra(..)
Start the new Activity: startActivityForResult(intent).
Inside new Activity you can get extra data with intent.getXyzExtra() (where xyz is type).
When new Activity is done just call setResult(int, resultIntent). Again you can add extra data as described in 1.
Call finish() to end the activity.
In original Activity method onActivityResult will be called. Again extract data from Intent as described in 3.