Android Home Shortcut loses flags after reboot - java

My Android app defines an activity with an intent filter of android.intent.action.CREATE_SHORTCUT, which lets me show up in the list of shortcuts that the user can add to their home page, when they select "Add Shortcut" from the menu or long-click the home page.
In this activity I have the following code (actually happens in a click event after they pick which shortcut to add):
Intent shortcutIntent = new Intent(this,MyActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.myicon);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppName");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
finish();
The shortcut works as I would expect, until I reboot the device. I'm actually testing on the emulator, not real device. The shortcut is still there after a reboot, so I know I didn't wipe user data or anything like that, but it acts like it no longer has the FLAG_ACTIVITY_NEW_TASK setting when clicked.
Example steps to recreate (assume my app is an email inbox for clarity):
Create the shortcut
Launch my inbox activity from my main menu acitvity, which uses the NEW_TASK flag.
From the inbox activity, click a message to open the view message activity.
Press HOME key
Click the shortcut -- at this point it brings the entire "task stack" back to the front, with the view message activity on top of the stack, clicking back goes back to the inbox activity, as I would expect
Reboot the device
Repeat steps 2 through 5.. now when I click the shortcut, instead of bringing the view message activity to the front, it brings the task to the front, but then adds a new inbox activity on top of the stack. So pressing BACK once goes back to the view message activity, and back again to the inbox activity.
I also tried setting different properties such as singleTask for my inbox activity in the app manifest, but haven't had any luck. Is this a known issue that flags are not saved with shortcuts?
I think I'll try adding a new stub activity that does nothing but launches the real activity with the NEW_TASK flag and then exits, and have my shortcuts point to that instead. However, seems like a lot of overhead, so hopefully someone has a better answer.

Related

Settings.ACTION_SOUND_SETTINGS is not going back to app

I am trying to open Sound settings from my app
startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS));
Sound Settings Activity displays without any issue.
Scenarios After Opening Sound settings page
When I press back button on my device, it is going back to my application.
when I click on back arrow button(top left), it is going to Settings Main Screen.
Issue:
I want to go back to my app when click on back icon also.
startActivityForResult(new Intent(Settings.ACTION_SOUND_SETTINGS), 100);
I have also tried this. but no result.
I am testing this on OnePlus 6 (Android 10 (Q))
Create an intent:
Intent intent = new Intent(Settings.ACTION_SOUND_SETTINGS);
Add these lines to your intent:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
then call startActivity(intent);

Initiliazing ParseLoginUI?

Where does one actually place the code to launch the ParseLoginUI activity?
ParseLoginBuilder builder = new ParseLoginBuilder(MainActivity.this);
startActivityForResult(builder.build(), 0);
Is it in the ParseLoginDispatchActivity? This was not made very clear at all within any of the official documentation:
https://github.com/ParsePlatform/ParseUI-Android
https://www.parse.com/docs/android/guide#user-interface
I'm importing ParseLoginUI into my existing app. What do I once I've installed everything, updated my manifests, my build.gradle and now want to actually launch the Login activity once my app launches?
Do I put something in my manifest to indicate that the ParseLoginActivity should launch first? That doesn't seem to work as an Activity from my main application is required to launch as the initial intent. I'm a little lost here... Any thoughts?
Well I did find one solution, albeit a trivial one:
Intent loginIntent = new Intent(MainActivity.this, ParseLoginActivity.class); startActivity(loginIntent);
I launched the above Intent with an options menu item, but you could do it with a button or whatever else suits your needs.
If you're importing ParseLoginUI into an existing app, it appears you can just launch ParseLoginActivity with a simple Intent. I wish they mentioned this on their integration tutorial. Seems like the most straightforward way to get it running.
This solution definitely launches the Activity you want, but it doesn't check for whether the user is logged in or not and hence doesn't redirect you to the appropriate pages in your log-in flow (which I believe has more to do with your Manifest). It does, however, allow you to successfully register a user and log in with Parse, which is a great start.
A better solution would be to add the following to the onCreate method in the Activity that launches when your app launches. So if when your app launches you land on FirstActivity, the following will check to see if you are logged in. If you are not, you will be sent the login screen, and if you are logged in you will be sent to the second Activity, which is presumably where your users will want to be when they open your app.
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
Intent launchMainActivity = new Intent(this, SecondActivity.class);
startActivity(launchMainActivity );
} else {
ParseLoginBuilder builder = new ParseLoginBuilder(FirstActivity.this);
startActivityForResult(builder.build(), 0);
}

Clear intent action default

I am launching the camera app in normal or secure mode depending on what gesture is performed using my app but once the user selects the app and taps Always then there is no option to change the defaults, even from the settings menu in Android.
camera_intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
secure_camera_intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE);
camera_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
secure_camera_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
this.startActivity(camera_intent);
//this.startActivity(secure_camera_intent);
Is there a workaround? I want to show the camera selection dialog once again so that the user can change his choice.
If you want to present the app chooser even if the user has a default activity selected for the task, use Intent.createChooser().
From the Android developer guide:
If you call Intent.createChooser(), passing it your Intent object, it
returns a version of your intent that will always display the chooser.
This has some advantages:
Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
If no applications match, Android displays a system message.
You can specify a title for the chooser dialog.
You should also check the documentation for ACTION_CHOOSER for guidelines on where this is appropriate or not (Intent.createChooser() is just a convenience method for this).
startActivity(Intent.createChooser(senderIntent, "Title here"));
It will always display the chooser dialog.

How do I create an android shortcut to an activity?

OK,I'm new at this forum, so don't blame me for putting this in the wrong tags,not putting something in,eg.
I want to learn how to create a shortcut(I did that by Googling) and link it to an activity (In this case, com.android.mms.ui.ComposeMessageActivity)
I tried doing it, but it only showed me a toast saying "Application not installed" and I'm pretty sure it is.
It would be better if you can display a "complete action with another application" dialog.
If I assumed your question correctly, you mean a button or something within an activity that leads to another activity, that being -- "com.android.mms.ui.ComposeMessageActivity"
if your activity that you want to link to is in another application-- then
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.mine", "com.android.mms.ui.ComposeMessageActivity"));
startActivity(intent);
if it is within the same application, then
Intent intent = new Intent(this, ComposeMessageActivity.class);
startActivity(intent);
//optional add this to your manifest to finish the current loading activity so
//as to not keep it in the activity stack
//<activity android:name="yourActivity" android:noHistory="true" ... />
EDIT If you mean a shortcut on a homescreen, then I would create a tiny application that only has one activity which uses the above method to link to a different application. Then I would drag that application to the home screen, and boom. If there's a better way, then please feel free to correct me

Button to start the Gallery on Android

I'm trying to make a button in my App open the built in gallery.
public void onClick(View v) {
Intent intentBrowseFiles = new Intent(Intent.ACTION_VIEW);
intentBrowseFiles.setType("image/*");
intentBrowseFiles.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentBrowseFiles);
}
This results in an error message "The application Camera (process com.android.gallery) has stopped unexpectedly."
If I set the Intent action to ACTION_GET_CONTENT it manages to open the gallery but then simply returns the image to my app when a picture is selected which is not what I want.
I'm trying to make a button in my App open the built in browser.
Your question subject says "Gallery". Your first sentence in the question says "browser". These are not the same thing.
If I set the Intent action to ACTION_GET_CONTENT it manages to open the gallery but then simply returns the image to my app when a picture is selected which is not what I want.
Of course, actually telling us "what [you] want" would just be too useful, so you are making us guess.
I am going to go out on a limb and guess that you are trying to open the Gallery application just as a normal application. Note that there is no Gallery application in the Android OS. There may or may not be a Gallery application on any given device, and it may or may not be one from the Android open source project.
However, for devices that have the Android Market on them, they should support an ACTION_VIEW Intent with a MIME type obtained from android.provider.MediaStore.Images.Media.CONTENT_TYPE.

Categories