Backpress to previous fragment in another activity - android-tabhost

It seems each activity has its own Fragment Manager but I have a situation when I need to access the Fragment Manager of a previous activity. Here is the situation. I have tabs A - E launching Activities A - E in each tab. Each activity have fragments. So I am in Fragment B1 (in Activity B launched by Fragment A3 of Activity A) and I want to go back to A3 but since A3 is in another Activity, I can't use popBackStack on BackPress because Activity B doesn't know (can't access) the BackStack of Activity A. How do I get back to A3 of Activity A from B1 of Activity B. Thanks

See when one activity calls another activity, the caller activity's instance state is saved in stack so if you want to go back from called activity to the caller fragment try calling finish(); in the back pressed method and it will do the trick for you.eg
#Override
onBackPressed{finish();}

Related

How to pass object between Android activities during back navigation

In my Android app I have two activities: Activity A and Activity B. The first one (A) starts the second one (B) with startActivity().
If the user hits the back button, I'd like Activity B to pass an object back to Activity A before it's destroyed. The only thing I can come up with is to override onBackPressed() in Activity B and save the object in SharedPreferences, then retrieve the object from SharedPreferences in Activity A's onResume(). Can anyone recommend a more elegant solution?
Thanks a lot!

Inter module communication using event bus in mvp4g gwt

Our web application is based on mvp4g framework. To explain my situation I will be using the following notations:
A, B - two different gwt modules
presenterA, viewA - presenter and view pair in module A
viewA contains an iframe and a button along with other UI components
presenterB, viewB - presenter and view pair in module B
eventBusA - an eventBus in A with event handler in presenterA
eventBusB - an eventBus in B with event handler in presenterB
Flow of application starts with loading of A. viewA is displayed and contains a button and an iframe on click of which module B is loaded inside the iframe by calling a URL and simultaneously hides the viewA from active viewing. Thus it is still active in the background. B is a seperate module(no child parent relationship between A and B). It loads viewB and after a button click in viewB I want to go again to an event in eventBusA since I want to change the status of viewA from hidden to visible. But eventBusA is not accessible from either presenterB or eventBusB.
I have tried the following which did not work:
Created and event in eventBusB to respond to click in viewB via presenterB. Added presenterA as the handler.
On the same event added moduleToLoad with module A as the target. It requires A to be declared as the child module.
Thought about javascript API using GWT-Exporter project.
Your problem is, that module B has to send a push notification to module A. There is no ready solution for that.
Best solution will be, to send an URL to start Module A again and use the Place pattern to restore the application state of module A.
Therefore module A has to send a token (history token) to module B. Module B has to save the token and if you module B will give back the control to module A, it has to call the url for module A and use the token as a #-parameter. Take a look at the history doumentation of mvp4g.

how to call previous activity in android?

I have five activity in my app,
Act1->Act2->Act3->Act4->Act5,
All activity goes in single direction as I have stated above,
Now I have to call again Act2 after successful completion of my Act5's task, so its working good and showing me the Act2 (I have Used startActivity(callIntent) in Act5 for showing me Act2 again),
After this when I click on Back button in Act2, I have Act1 (I have Used startActivity(callIntent) in Act2 for showing me Act1 again) its what I want .....
But the problem is that when I click Act1's back button it is going to put me on Act2. But I want to exit from there, because Act1 is the first initial activity.
How Can I set focus to my hidden activity , instead of creating Intent and call startActivity.
Any idea? Please help.
After long research I got pretty much understanding of "back stack" for all the activities in an app.
Each time I moved to another activity and also for opening previous activity I have used Intent.startActivity() with flag FLAG_ACTIVITY_NEW_TASK, So every time I have new activity added in the "back-stack".
I have tried intent flag as FLAG_ACTIVITY_CLEAR_TOP while calling previous activity from "back-stack" using Intent.startActivity() and I got the solution of calling back the previous activity and clears all stack top activity above the calling one activity. So now back button does working nice as I needed.
Let see My problem of calling previous activity ,
A = activity; A1->A2->A3->A4->A5->B2 , now if you click on back button you will have A1 activity , and after clicking on A1's back button you have again B2 activity which was called after A5 and so on.
After using the FLAG_ACTIVITY_CLEAR_TOP in A5 activity and called again A2 (Not creating new activity but calling precious one) I have following situation.
A = activity; A1->A2->A3->A4->A5
calling previous A2 activity, and I have following scenario.
A1->A2 only.
Thanks.
Instead of following code (with flag Intent.FLAG_ACTIVITY_NEW_TASK):
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(CurrentActivity.this,PreviousActivity.class);
startActivity(callIntent);
Try using following one with Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
callIntent.setClass(CurrentActivity.this,PreviousActivity.class);
startActivity(callIntent);

How to finish an Activity that is used in a TabSpec, as the child of TabHost

I have Main Activity which contains a TabHost.
In the TabHost I have several TabSpec which use an Intent for the content of the tab.
At certain points in my code I need to remove a tab from the TabHost, and finish the Activity that was in that tab's TabSpec.
I am able to remove the tab from the TabHost, but as soon as I call finish on that child Activity, the Main Activity terminates. I have even tried calling finish some time later, with a timer. It still kills the main activity.
How can I finish a child activity without killing the main activity?
Tab Host was decrapated by google so you shouldnt use it anymore you should replace it using actionbarsherlock its a better option for tabs.

Lifecycle and Activity Stack

In the application activities are stacked like this: A - > B - > C - > D - > E.
If I receive a particular notification and click on it, Activity E is started.
If I then click back (button on phone or button on actionbar), the application exit.
How do I make the transition to Activity D in this case, and then back through C, B, and A?
My code of back button:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
Everything is okay when starting the application normally. The problem is when Activity starts from the notification.
Android has the functionality you're after built in, and it is already well documented. To begin with you should look at the TaskStackBuilder class. It was introduced in JellyBean, but is already included in the support library, and you use it to build a synthetic TaskStack which is what you need. A summary from the documentation reads:
When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.
TaskStackBuilder provides a way to obey the correct conventions around cross-task navigation.
How you build it is going to depend on the relationships of the Activities in your app, but the Tasks and Back Stack developers guide is a good read to help you decide, as is the Navigating with Up and Back design guide, if this is all new to you.
You'll find some code examples in the Implementing Effective Navigation lessons, also on the Android developers site, in the training section.
Incidentally, the button on the ActionBar is referred to as Up. Even though it sometimes shares the same functionality as the back button, the two are not the same (I assume that's the one you are talking about ;-) .)
I think you can solve your problem by sending an intent from Activity E to Activity D, and so on.
Therefore you should overwrite the method
onBackPressed()
that is called when you click on the back button.