Reputation: 542
i have small practice app. i put some dynamic shortcut for my app.all shortcut works like i can able to open site from shortcut(google docs example) but now i want to open particular activity from this shorcut .so how can i do this.
and here is code of my work.
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//open activity.
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
ShortcutManager shortcutManager= (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutInfo shortcutInfo=new ShortcutInfo.Builder(MainActivity.this,"shortcutID")
.setShortLabel("Shortcut Label")
.setIcon(Icon.createWithResource(MainActivity.this, R.drawable.ic_message))
.setIntent(new Intent(Intent.ACTION_VIEW))
.setActivity()
.build();
shortcutManager.addDynamicShortcuts(Arrays.asList(shortcutInfo));
}
}
});
so how can i open activity from first shortcut selection .waiting for reply
Upvotes: 0
Views: 2891
Reputation: 503
Create your intent for a specific component with a specified action and data like this.
Intent intent = new Intent(Intent.ACTION_VIEW, null, MainActivity.this, Main2Activity.class);
And your shortcut set it with
.setIntent(intent)
Upvotes: 2
Reputation: 1049
You have a couple of options to launch an activity from a dynamic shortcut.
In your .setActivity()
, you can pass the desired activity class.
Or if you app links setup with intent filters to that activity, how I've done it passing in a URI.
setIntent(Intent(Intent.ACTION_VIEW,
Uri.parse("youapplication.uri.com/myintentfilter)))
Or you can launch multiple intents if you wanted activities to be on the backstack by using the setIntents()
JingJoeH also had a good answer that is slightly simpler in implementation https://stackoverflow.com/a/48092366/7900721
Upvotes: 0