How to pass intent from TabActivity to its child activity - android

I need some help :)
I have a tab activity with four sub activities. First and most important is a list activity, second also list, third is settings activity and fourth with help.
When app is launched tab activity creates specs with intents for sub activities and sets first activity as a current tab so that activity acts upon intent from spec and that works fine. Later a broadcast receiver can broadcast notification with intent to refresh first list activity because new data has arrived.
My goal is: When the user acts upon notification: If app is launched, so there is a task on stack but not on top, to refresh that existing list activity inside tab and bring the whole app to front. If app is already visible but on different sub activity (different tab) just to navigate to first tab and refresh first list. If first list (first tab) is already visible just to update it.
I can't seem to make this work. I've tried different strategies. Intent that targets tab activity. Intent that target sub activity. Combinations of launchMode and intent flags but i cannot make this work. It seems to me as a reasonable scenario.
Any insight will be appreciated.

OK this workaround worked for me:
TabActivity has an onNewIntent(...) method which handles intents that I want to pass to child activity. TabActivity will call a method on custom application object. That object will pass message to ChildActivity which passed it self as a listener on app object on creation.

Related

Android: i need to navigate to a same activity through different activities and returning back to those activities after im finished

I have two activities one is saved address activity and the other is a cart activity both have a button to navigate to the same page which is map to get the location and after finding the location their redirected to another page which is to fill their house no and after filling them it has a button when clicked I want an event to get back to their starting activities
I used intent extras to put information as key values 1 and 2 in the corresponding pages and checked it at the end but still no use
If I have understood correctly you want to send the information (location, house no) back to your starting activity?
One "dirty" solution would be like:
Intent mIntent = new Intent(mContext, YourDestinationActivity.class);
mIntent.putExtra("location", location);
mIntent.putExtra("houseNo", houseNo);
startActivity(mIntent);
But for this kind of workflow I would use Fragments
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

Start new activity or bring up old one according to intent extra

What flag must I set for an intent so if the activity is visible with same extra it does not bring up a new one.
My app is somehow like google play. There are different app that are show in AppActivity by sending the app id as an Extra. User can download app and when download is finished, a complete notification is shown which on press brings up the corresponding activity page.
Currently by pressing notification it start a new activity which is correct for when AppActivity is not visible or showing another app. But it also start a new AppActivity even if the AppActivity is visible for same app.
PendingIntent.getActivity(ServiceDownload.this, id,
new Intent(ServiceDownload.this, AppActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
.putExtra(Constants.EXTRA_ID, id), 0);
Use Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP while starting your activity and define it single top at your manifest file.
If activity is somewhere in the stack, it will be brought to top(onNewIntent method will be invoked) and all activities above it, will be destroyed. If it is not in the stack it will be created (onCreate method will be invoked)
Edit: You have to handle onNewIntent to get bundle and update your current activity with new values.
Note: Also remember, system may destroy your activity and your both onCreate and onNewIntent methods can be invoked. Design your activity considering this.
Edit2: If you want to have many app details and user can navigate back with pressing back button. And also one detail for one app. Then you should use fragments. Add fragments by tag of your application id/name (something unique) and while showing an app detail search if any detail page exists with findFragmentByTag method. If it exists remove it and add it to top of the stack.

launching already launched activity from notification creating problematic activity

i have 3 activities A-B-C .Lets say i am on activity B and from a listview there i launch activity C now if a notification comes which has an intent of launching activity C.
So the problem is I am getting multiple instances of activity C
When i use launchMode singleTop or instance or task the problem of multiple activity instance is solved but the newly launced activity does not work properely as it is desired to be.
Please help me out tired of using flags and stuff but not able to overcome this problem.
The scenario is Just like whatsapp , if u r talking to person one and a message of person 2 come as notification ,when u click on that notification the activity relaunches and works properely. This is exactly what i want. Please help me out how to achieve this . :(
Thanxx in advance
What Flags did you try and what exactly is not working, means, how does the Activity behave?
What you describe with WhatsApp could be achieved with two steps:
Use the FLAG_ACTIVITY_SINGLE_TOP for the Activity.
Overwrite the Actvity.onNewIntent(Intent intent) method in the Activity. This method is called instead of creating a new Activity and gives you the new Intent. I assume that the Intent contains all necessary information in the extras.
The user experience should be as follows:
User chooses from the list, which opens the new Activity
He/she can press home or back button from here. This brings the home screen or the list back.
If, for any reason, somebody else calls startActivity for your Activity while it is running, the onNewIntent is called. There you can add code to refresh the content, maybe have an Alert that tells the user that the content has changed, and shows all that to the user.
If the user presses back or home now, he/she will get to the list or home screen
If that is, what you're looking for, then the flag and the method is what you need

Managing Android Activity stack: bring specific activity instance to front

I have a text message app which has two main activities:
TextChatActivity - a conversation. Can have multiple instances for multiple conversations.
ConvListActivity - a list of all the activities.
In addition, a TextChatActivity of a certain conversation can be opened via a status bar notification.
What I want to do is to bring a specific TextChatActivity instance to the front.
Let's say I have 2 open conversations (TextChatActivity) A and B, and B is on front.
Now I got a notification that leads to conversation A. I want it to bring conversation A TextChatActivity to front.
How can I do that without opening a new instance of TextChatActivity for conversation A?
Thanks!
Actually, you can't. If you have multiple instances of an activity in the stack, there is no way to address a unique instance of an activity so that you could bring it to the front.
Your architecture is not good. Because of the way Android works, you would be better off if you had a single instance of this activity, and allow the user to switch between conversations, not by creating a new instance of an activity, but just by switching out the underlying data for the existing activity. In this way, you only ever have one instance of the activity and you can simply change the data that you are displaying.
In your "notification" example, the Intent that you start from the notification should have an "extra" that indicates which conversation the user wants to show. You should ensure that there is only one instance of your TextChatActivity by declaring it with launchMode="singleTop" or by setting FLAG_ACTIVITY_SINGLE_TOP when you start it. When onNewIntent() is called in your activity, check the "extras" and adjust the content to show the desired conversation.
If you want to create the "illusion" of an activity per conversation, then you can override `onBackPressed() and manage your own "stack" of conversations in the activity, so that when a user presses the BACK key, you can go back in the "stack" of conversations and show him the previous one, just by modifying the data that is displayed.
Just start an activity and set the flag FLAG_ACTIVITY_CLEAR_TOP

Accessing sibling tabs in an Android application?

I have an Android application using a TabHost to create several tabs with different activities in each tab.
When someone performs a certain action in the activity associated with tab A, I want to programatically switch to tab B, and call a function on tab B's activity to provide it with some data generated in tab A.
Is such a thing possible? I don't know how to access either the TabHost object or the B activity from within the A activity. I tried passing these objects in to the A activity by adding them as "extras" to the Intent, but this doesn't work, probably because the classes are not serializable. If I could access the TabActivity itself, that would probably be the best solution.
Thanks,
Frank
Implement a BroadcastReceiver in the TabActivity and another in Activity B.
Register both in the manifest with custom intent filter 'actions'
Get Activity A to send a broadcast with an Intent including extras that the TabActivity will receive.
Have the TabActivity switch tabs then send a broadcast with an Intent with extras that Activity B will receive/process.
To expand on this, think of it this way...
Under 'normal' circumstances an Activity is a stand-alone / self-contained entity. If it needs to have some action performed by another Activity, it shouldn't need to know anything about that other Activity, just a way to 'call' it, i.e., use an Intent set with the relevant action/category and extra data.
In the case of an Activity which is embedded as tab content, this mostly still applies. The Activity itself doesn't 'know' that it has been embedded as tab-content (nor should it). As such it knows nothing about the TabHost or Tabactivity (when using one). It also doesn't/shouldn't know specifically that there are other tabs with other Activities...
It is quite legitimate, however, for the TabActivity to know about the tab contents (it created them after all) and to be able to mediate between them. If you simply have Activity 'X' broadcast that a particular action has been performed and allow the TabActivity to receive and act on the Intent type/data then the various siblings don't need to know specifically how each other works.
That would be my approach anyway. :-)
You could stash the data in an object at the Application level. Then when activity B starts it could check for the data.

Categories

Resources