Launch Fragment (instead of Activity) from Android 7.1 App Shortcut

邮差的信 提交于 2019-12-05 03:53:43
Tin Tran

You can do the following:

1) In the intent specified in shortcuts.xml, set a custom intent action string:

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

2) Check for the getIntent().getAction() for intent action in the Activity specified in android:targetClass:

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

You can easily achieve this with the Shortbread library.

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

Add Shortbread.create(this) in the onCreate() method of your Application or MainActivity and you're done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!