Launch Fragment (instead of Activity) from Android 7.1 App Shortcut
You can easily achieve this with the Shortbread library.
class MainActivity : AppCompatActivity() {
@Shortcut(id = "movies", shortLabel = "Movies", icon = R.drawable.ic_shortcut_movies)
fun showMovies() {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, MoviesFragment())
.commit()
}
}
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
}
}