How do I create an android shortcut to an activity? - java

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

Related

How to properly close login page?

For example, first activity of app is MainActiviy which is basically 2 text fields (login and password) with a button. Pressing a button tells the database these text fields' data so it can tell the program if the user is in database and password is correct.
What I'm used to is to create a bool flag field saved in SharedPreference so the program knows i already logged in my app recently so it can open my second activity, but it seems like wrong and weird solution.
So, how do I properly login into my app in theory?
Mikkel,
Your approach with storing data using shared preferences is perfect but I have some suggestions which can make this solution better and robust.
First of all, you should check the sharePreference value inside a splash screen. Which checks for the login flag and launch the LoginActivity if a user is not logged in and launch the HomeActivity if a user is logged in.
Second, I would suggest using Androidx DataStore, which is a better version of shared preference and it can be asynchronous API so you can use it safely on the UI thread.
Don't forget to close your main activity after user login using the below code.
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If you need an example to handle this stuff better here is the github-sample which can be very helpful.
This is wrong:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
You should use "startActivity" before "finish()"
Correctly:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

How to open particular activity using shortcut?

I am trying to open particular activity using shortcut but always run application.
When application is running in background and click on shortcut icon -> particular activity is open.
If application is killed from background then how to open particular activity using shortcut.
* Below method use for create shortcut
private void createShortcutOfApp() {
Intent shortcutIntent = new Intent(getApplicationContext(),
YourTargetActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "App shortcut name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.logo_of_your_app_shortcut));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
permission in manifest
uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT
define in manifest
android:name=".YourTargetActivity"
android:exported="true"
I am tired to solve this problem, please help me.
Follw below link to know how to open particular activity on relaunch application.
It works for me.
How to make an android app return to the last open activity when relaunched?

Is an Android Intent equivalent to a Java Event?

I'm trying to understand Android Intents and I understand the concept of a Java event and an EventListener. Are these similar or the the same?
Intent in Android, is used to express an "intention" as the name implies, to perform an action over another component. One common usage of intent is to use it along with startActivity(intentObject), where the object contains the information(contexts) of the starting and openning activity, see below:
Intent intent = new Intent(context, MyActivityB);//pass the information(contexts of the current activity and the activity I want to open).
startActivity(intent);//Use startActivity method to start the activity defined in the object intent.
Other actions include: openning an app to send an email, open a social media, etc.

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);
}

Android Home Shortcut loses flags after reboot

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.

Categories