random1132
random1132

Reputation: 101

Opening a fragment via quick app shortcut

Hello, I do know that this is a duplicate question, but i was not able to find the solution from the other threads.

Here is my problem, I have an app with bottom navigation bar with five fragment. I want to open the those fragment via the app shortcut.

Here what i have created,

shortcut.xml

<shortcuts xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:shortcutId="first"
    android:enabled="true"
    android:icon="@drawable/shortcut_1"
    android:shortcutShortLabel="@string/shortcut_short_label_one"
    android:shortcutLongLabel="@string/shortcut_long_label_one"
    tools:targetApi="n_mr1">
    <intent
        android:action="com.xxxxxxx.xxxx.FIRST"
        android:targetPackage="com.xxxxxxx.xxxx.test"
        android:targetClass="com.xxxxxxx.xxxx.test.MainActivity" />
</shortcut>
<!-- Specify more shortcuts here. -->
<shortcut
    android:shortcutId="second"
    android:enabled="true"
    android:icon="@drawable/shortcut_2"
    android:shortcutShortLabel="@string/shortcut_short_label_two"
    android:shortcutLongLabel="@string/shortcut_long_label_two"
    tools:targetApi="n_mr1">
    <intent
        android:action="com.xxxxxxx.SECOND"
        android:targetPackage="com.xxxxxxx.xxxx.test"
        android:targetClass="com.xxxxxxx.xxxx.test.SecondFragment" />
</shortcut>
</shortcut> 

Here is the onCreate() from the MainActivity

private static final String Second = "com.xxxxxxx.xxxx.SECOND";

    //code vomited  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Second.equals(getIntent().getAction())){
            SecondFragment secondFragment = new SecondFragment();
            android.support.v4.app.FragmentTransaction fragmentTransactionTwo = getSupportFragmentManager().beginTransaction();
            fragmentTransactionTwo.replace(R.id.content_main, secondFragment, "Fragment two");
            fragmentTransactionTwo.commit();
        }

        BottomNavigationView navigation = findViewById(R.id.navigation);
        BottomNavigationViewHelper.disableShiftMode(navigation);     //disabling the shitty shifting mode
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        FirstFragment firstFragment = new FirstFragment();
        android.support.v4.app.FragmentTransaction fragmentTransactionOne = getSupportFragmentManager().beginTransaction();
        fragmentTransactionOne.replace(R.id.content_main, firstFragment, "Fragment one");
        fragmentTransactionOne.commit();
    }

Here the first activty launches fine i.e MainActivity(). But Click on the second shortcut nothing happens.

Anyone explain me what am i doing wrong?

Upvotes: 4

Views: 1297

Answers (3)

Jonas.S.
Jonas.S.

Reputation: 317

After some time I found the answer to how to open the fragment via the shortcut Kotlin:

shortcut.xml

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:shortcutId="mainShortcut"
    android:enabled="true"
    android:shortcutShortLabel="@string/somestring">
    <intent
        android:action="xxx.example.workapp.test"
        android:targetPackage="xxx.example.workapp"
        android:targetClass="xxx.example.workapp.MainActivity">
    </intent>
</shortcut>

MainActivity

class MainActivity : AppCompatActivity() {

private lateinit var bottomNav : BottomNavigationView

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    loadFragment(HomeFragment())

    if ("xxx.example.workapp.test" == intent.action) {
        loadFragment(YourFragment())
    }

    bottomNav = findViewById(R.id.bottomNav)

    bottomNav.setOnItemSelectedListener {
        when (it.itemId) {

            R.id.homeFragment -> {
                loadFragment(HomeFragment())
                true
            }

            R.id.SettingsFragment -> {
                loadFragment(SettingsFragment())
                true
            }

            else -> {
                false
            }
        }
    }

}

 private fun loadFragment(fragment: Fragment){
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.container,fragment)
    transaction.addToBackStack(null)
    transaction.commit()
}}

AndroidManifest.xml

...
<activity
        android:name=".MainActivity"
        android:configChanges="locale"
        android:exported="true"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />

    </activity>
...

Upvotes: 0

pipin syaepudin
pipin syaepudin

Reputation: 11

i have same problem like yours, i found how to move to fragment. you should hit the action. put this in onCreate, maybe this will help you

if ("com.xxxxxxx.YourShortcutAction".equals(getIntent().getAction())) {
        val fragment = YourFragment()
        addFragment(fragment)
    }

im using kotlin for android

Upvotes: 1

jmart
jmart

Reputation: 2941

I see a couple of things that don't look right:

On shortcut.xml:

Both shortcuts should target the 'MainActivity' class, as this is the point of entry and the responsible for adding the fragments to the ui. So, you need to change the targetClass on the second shortcut and target the 'MainActivity' instead.

On the onCreate method:

When the action corresponds to the second shortcut, it sets the second fragment on the screen but the method goes on and sets the first fragment over it.

I guess it should be an if-else:

if (Second.equals(getIntent().getAction())) {
    SecondFragment secondFragment = new SecondFragment();
    android.support.v4.app.FragmentTransaction fragmentTransactionTwo = getSupportFragmentManager().beginTransaction();
    fragmentTransactionTwo.replace(R.id.content_main, secondFragment, "fragment two");
    fragmentTransactionTwo.commit();
} else {
    FirstFragment firstFragment = new FirstFragment();
    android.support.v4.app.FragmentTransaction fragmentTransactionOne = getSupportFragmentManager().beginTransaction();
    fragmentTransactionOne.replace(R.id.content_main, firstFragment, "Fragment one");
    fragmentTransactionOne.commit();
}

Upvotes: 1

Related Questions