starting first activity from a child activity in TabGroupActivity in android - android

My application contains four tabs at the bottom.Each tab has multiple child activities.The flow may be like this,
Tab1--A-->B-->C-->D-->E
Tab2--X-->Y-->D-->E
Tab3--M
Tab4--P-->Q-->Y-->D-->E
My question is ,
when I am in C child activity of Tab1,and I press Tab2.Again when I come back to Tab1 ,it is in C child activity.But I want A activity to be restarted.can someone please give the solution by providing some sample code.Thank in advance

from your B activity, start C activity using method, startActivityForResult() and Override onActivityResult() method in all of them this way.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
finish();
}
Now at every Tab switch, you must call finish() at current active activty. that in turn will destory all activities and coming back to that tab will show you first activity.
Note that, you wont want to destroy your first activty. For this reason, you must start activity B from A using startActivity() and you donot need to Override onActivityResult() for A activity.
regards,
Aqif Hamid

Related

Android Backbutton Pressed Return to Two Activities

This may sounds simple, but I am so bewildered by it. I have searched but not found any solution.
My question is: How to return to two activities when back button pressed?
Like this: let me say that I have activities A, B and C (A -> B -> C). What I want to achieve is when I am on activity C and press the back button, It should return me to activity A. When I am on B and press back, it should return me to A too.
It may be implemented into a project with many activities, so I assume that I don't need to set the class name of where to return, It should be recorded automatically by the android. How to achieve this?
Thank you
A possible solution is calling startActivityForResult() from Activity B, so that on the callback of the created Activity C, the previous Activity B gets finished as well. The callback function is called onActivityResult(), and in that, you want to call finish().
Example:
ActivityB:
Intent i = new Intent(this, ActivityC.class);
startActivityForResult(i, 0);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
this.finish();
}
ActivityC:
//do stuff
This way, when you press Back (or call finish()) in ActivityC, it will call back on ActivityB's onActivityResult() function, and it will end them both.
You can override the onBackPressed method and sent an intent to the activity you want.
#Override
public void onBackPressed()
{
// code here to send intent to the activity A
}
One thing, I'm not sure if this goes well with the activity stack but you can alway try.
you can finish activity B , when you are starting intent for activity C, then activity stack will have activity A, and when you press back on activity C, activity A will be displayed.
just override onBackPressed in Activity C, and finish() it.
You can call the Activity that you want to go back to with a special flag (FLAG_ACTIVITY_CLEAR_TOP) from your onBackButtonPressed which will skip/remove the other activities in between. This way you can go back from C to A.
See: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
I think this is better than messing around with the finish() or starting activity for result when there is no result to return.
A --> B --> C
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will launch a new intent for Activity A and clears all the other activities from Stack.

return back and skip one activity android onbackpressed

Here is my case
assuming that I have those activities
A -> B -> C -> D
I want to come back to A when the user click on the back button when he is in D
but also I want to come back to B when the user click on the back button of C
I thought to call finish when I go to C from B, but in this case I can't return to B if I click on back of C
how can I fix this issue without calling onBackPressed on two activities?
thanks
When you start each activity, you will need to use
startActivityForResult()
And then have a system of flags saying who is being stopped which are passed back. So, activity D:
timeToEnd() {
setResult(RESULT_D_CLOSING);
finish();
}
And then in A, C, D
onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode)
case RESULT_D_CLOSING:
// Close on upwards
setResult(RESULT_D_CLOSING);
finish();
case ....
// You get the idea
}
}
Have you tried using FLAG_ACTIVITY_CLEAR_TOP flag in your intent (used to start "A" from "D")?
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
You have 2 options here. You can either start the activities with startActivityForResult and then onBackPressed of each activity pass a flag back that tells the activity to either exit of stay open.
An other option would be to implement A,B,C,D as fragments in the same activity put all the fragments on the backstack as you open them and then onBackPress you can programmatically loop through the backstack to see what you want to do.

Android - Back Button to restart Activity

I have 4 Activities and my navigation is like this :
first activity --> second activity --> third activity --> fourth activity --> third activity
when I press back now from the 'third activity' , it goes to the fourth activity . However I want it to go to the second activity. So, I have overridden onBackPressed() method in third activity to : startActivity(this , SeconActivity.class)
Problem : When I first navigate from first activity --> second activity , I enable and disable buttons based on some inputs from the first activity in the onCreate() method. So, I want to preserve these changes when I navigate from third activity --> second activity . Any ideas ?
Start the 4th activity via a startActivityForResult. When you want to call the 3rd activity again, just return the results required to configure the 3rd activity as you want it to be, via an onActivityResults().
3rd activity psuedo-code:
final int FOURTH_RESULTS=101;
startActivityForResults(Intent fourthActivityIntent, FORTH_RESULTS
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case FORTH_RESULTS:
//Do stuff here
break;
}
}
4th activity psuedo-code (Place this where you used to call the 3rd level)
Intent result = new Intent();
setResult(Activity.RESULT_OK, result);
See also the API.
Read about the activity launch modes for info on this subject, singleTop has worked for me in the past.
http://developer.android.com/guide/topics/manifest/activity-element.html
If you need to do complicated flows I recommend using this library
http://www.androidviews.net/2012/11/wizardpager/
It is a good way of enforcing a sort of flow into your android app.

Proper way to close/finish Activity in Android

My app starts up at Activity A which contains a ListView. The ListView can have items added to it if the user hits "Add" button and goes to Activity B.
In Activity B they fill out some forms and hit "OK" button which takes them back to Activity A where the new item is added to the ListView.
I have a finish() method after going from B to A -- but NOT the other way around.
So if you hit back three times after adding three items. It will just repeat the ListView (Activity A) over 3 times -- less one item that was added.
What is the best way in doing this? I can't put a finish method on the "Add" Button (going from A to B) because if you are in Activity B, it will close the app instead of taking you back to A -- which I do not want. That is, if the user changes his mind and doesn't want to "Add new item" by hitting "OK" while in B. Is a manual Back button the only answer?
Start Activity B by using startActivityForResult() and finish activity B after filling the form.
EDIT
When you startActivityForResult(), you pass 2 parameters, namely intent and requestcode. After you are finished with the new activity(in your case Activity B) you use a function setResult(RESULT_OK) to signify that the operation in Activity B was successful and then you call finish(). After the call to finish() the Activity B will return to Activity A and will call onActivityResult(int requestCode, int resultCode, Intent data). The parameter requestcode helps in identifying which particular activity/request has returned.
Hope this explanation helps you.

Android - finishing activity from another activity

is there any way, how to finish certain activity from a stack? I have service, which looks for updates, and when update is found, it opens update activity, where prompt for installation will appears. But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.
Thanks
If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}
very helpful code
classname.static object.finish();
Try this please.
In first activity, define this before onCreate.
static TabsViewPagerFragmentActivity activityMain;
Then, in onCreate of first activity, write this.
activityMain = this;
Then, in second activity, write this when you want finish first one.
TabsViewPagerFragmentActivity.activityMain.finish();
This will destroy first activity and when you press back on second activity, you will go back to home directly.
Use finish() method of the activity class to finish certain activity
this.finish(); // you can also use the application context instead of this
Hope this will help you.
But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.
When your "update activity" calls startActivity() to bring up "the installation", have it immediately call finish() on itself.

Categories

Resources