I know it sounds erroneous, "using intent to launch a new fragment?". You may say it should be activity, not fragment if we are talking about Intent. But according to the JetPack Preferences documentation we can use Intent to launch a new fragment.
Set an Intent
You can set an Intent on a Preference to launch a new
Fragment, Activity, or separate application whenever the Preference is
tapped. This is the same as using Context.startActivity() with a given
Intent.
As you can see no example provided about launching fragment with Intent.
If there is a way to achieve this i would like to learn.
Because I am developing a single activity app with fragments, there is navigation component and bottom navigation menu. Trying to use a new activity changes lots of things.
I am just trying to open another fragment when user tap a Preference.
Related
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).
I am going through the Intent on the android API page but I dont really get which flag to use. I want to set a FLAG to an intent that: if that activity is already launched it is going to bring it back on top without creating a new instance of it. If the activity is not launched then I need a new one to start and brought on top. What is the right FLAG to use?
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.
Is it possible to open google navigation intent inside my appln (in a view like a fragment). I would like to display other info to the user as the navigation is going on.
No the intent starts an activity which means you leave your app completely and go to the google navigation app
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.