Run snippet (instead of activity) with Android 7.1.

I decided to look at adding static shortcuts to the application using this page as a link:

https://developer.android.com/preview/shortcuts.html

The XML for my shortcuts currently looks like this:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="id"
        android:enabled="true"
        android:icon="@drawable/icon"
        android:shortcutShortLabel="@string/short_label"
        android:shortcutLongLabel="@string/long_label"
        android:shortcutDisabledMessage="@string/disabled_message">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example"
            android:targetClass="com.example.Activity" />
        <categories android:name="android.shortcut.category" />
    </shortcut>
</shortcuts>

The problem is related to the variable targetClasssince I can not find a way to start Fragmentand not Activity. Most of the main pages that I would like to launch from shortcuts are those Fragmentsdisplayed in Activity. How can I get intentstart right with Fragmentinstead?

+8
source share
2 answers

:

1) , shortcuts.xml, :

 <intent
        android:action="com.your.packagename.custom.name"
        android:targetPackage="com.example"
        android:targetClass="com.example.Activity" />

2) getIntent().getAction() , android:targetClass:

public void onCreate(Bundle savedInstanceState){
    if (CUSTOM_NAME.equals(getIntent().getAction())) {
        // do Fragment transactions here 
    }
}
+22

Shortbread.

public class MainActivity extends Activity {

    @Shortcut(id = "movies", shortLabel = "Movies", icon = R.drawable.ic_shortcut_movies)
    public void showMovies() {
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new MoviesFragment())
                .commit();
    }
}

Shortbread.create(this) onCreate() Application MainActivity, .

+3

Source: https://habr.com/ru/post/1658958/


All Articles