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.
Related
I have activities like A->B->C->D. How can I close the A activity if I have 4 activities on my stack? Also later when I open activity E i want B to be closed aswell, so I want to have C->D->E only.
There is no such as a direct way to manage activities number in stack. So far I know that stack is big as much as available memory.
Also consider LaunchMode and whether activities are in the same task or not.
So, you might implement your own Activity manager to finish un-wanted activities.
Here is briefly how I see the solution:
Create a model to store activity, its index in stack, date ..i.e. ActivityItem
Create an empty List of ActivityItem in your custom Application. To avoid memory leak, use WeakReference. create a public setter adActivity to add and manage activities
Better approach to create activity base class and reuse it as superClass wherever you want to manage count of running activities. instead of repeating same implementation for each different activity.
OnCreate in your base Activity call adActivity and pass current activity.
adActivity job is firstly clean the list of destroyed activities. thanks WeakReference. Then manually kill older activities before last 3 items in your list. It's not easy as it looks.. for example: SingleInstance and rotation will make it challenge to achieve this :-)
that is it.
Good luck,'.
When you start activity use
startActivityForResult(intent,code);//different code for all activity
and call
finishActivity(code);//which activity do you want to close?
You can call:
finish();
after:
startActivity(intent);
In every activity that you want to close, when you open another activity.
I know many of you will direct me to the API. I am getting confused the more I read about Intent Fragment and Activity. Can anyone please describe what are these and why are these three important for the process of android application development?
Thanks for you help in advance.
1) Intent : -
It's an "intention" to do an action. It is like sending Message to Android OS to carry Out some task. For ex: Start other activity if some action happens. See Below Links :
http://developer.android.com/reference/android/content/Intent.html
2) Activity : -
It is a Single Screen that users Interacts with.It is the only component that can (and must) have a user interface. Learn Activity Life cycle. It is Very important. You Should declare your activity in manifest File.
3) Fragment : -
A Fragment is a behavior or a portion of user interface in an Activity. We can call it like sub Part of Activity.Just Remember that Fragment May or may not have view. It is Like Small Activity,but they can be multiple on single screens and we can interact with them. Read the Fragment Doc from Developers site. It is great place to Start. see this : - http://developer.android.com/guide/components/fragments.html
In Simple Words,
1) Activity: is a screen which hold view(s) for GUI components. A Window in Desktop Application. It has a lifecycle like created,paused, stopped like in window.
2) Fragment: is a component used for dynamic GUI development. it also has a own lifecycle. But only difference is that it can't be used directly. it should be encompassed in Activity in order work. An activity may have one or more than one fragments.
3) Intent: is a message passing framework from one activity to another. message can be anything causing an activity to resume, passing extra to an activity or cause an application to start.
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)
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
If you have several activities onPause() is there a way to finish a specific activity?
edit: so for example, imagine on start activity 1 is called. Then activity 1 uses an intent to go to activity 2. then an update is made to the database and calls activity 1_new again so that it displays the updated database. At that point i want to get rid of the old activity 1.
It depends on what you want to do. You'll need to look at the AndroidManifest.xml spec for activity calls stacks.
Specifically android:launchMode
<activity android:launchMode="singleTop">
Careful though, launchModes are very tricky and can get you into trouble since it also depends on how the activity is launched from the Intent itself.
singleTop will essentially keep only 1 instance of that activity in the stack.
From the Docs:
If an instance of the activity already
exists at the top of the target task,
the system routes the intent to that
instance through a call to its
onNewIntent() method, rather than
creating a new instance of the
activity.
What I ended up doing here was calling startActivityForResult in the first activity. That way I was able to redisplay updated information from the second activity.