1

The structure of activity and fragment is like :

Using NavigationDrawer.

MainActivity has a container for Fragment, in one container there should be have many fragment. After launch the app, default of fragment is fragment A.

Implement deeplink into MainActivity with Fragment B, how I can change the fragment from Fragment A(default) into Fragment B (destination).

Any help will helpfull :) Thanks

i.e class handle Applink

    override fun onInitView() {
    handleIntent()
  }

  private fun handleIntent() {
    // ATTENTION: This was auto-generated to handle app links.
    val intent = intent
    val appLinkAction = intent.action
    val appLinkData = intent.data
    if (appLinkData != null) {
      handleAppLinkIntent(appLinkData)
    } else {
      handleActivityIntent(intent)
    }
  }

  protected abstract fun handleActivityIntent(intent: Intent)

  protected abstract fun handleAppLinkIntent(appLinkData: Uri)

impl function

    @Override
  protected void handleActivityIntent(Intent intent) {
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
      switch (this.paramMainMenu) {
        case Constants.ZERO:
          this.goToProductList();
          break;
        case Constants.ONE:
          this.goToFragmentA();
          break;
        case Constants.TWO:
          this.goToFragmentB();
          break;
        case Constants.THREE:
          this.goToFragmentC();
          break;
      }
    }
  }

@Override
  protected void handleAppLinkIntent(Uri appLinkData) {
    if(appLinkData.getQueryParameterNames().isEmpty()){
      String path = appLinkData.getPath();
      switch (path) {
        case "/kfc/food/price/outofstock":
          this.paramMainMenu = Constants.ONE;
          break;
        case "/food/price/qty":
          this.paramMainMenu = Constants.THREE;
          break;
        case "/price/unbuyable":
          this.paramMainMenu = Constants.TWO;
          break;
        case "/price":
          this.paramMainMenu = Constants.ZERO;
          break;
        case "/food/price":
          this.paramMainMenu = Constants.ZERO;
          break;
        case "/a/food/item":
          this.paramMainMenu = Constants.ZERO;
          break;
        case "/":
          this.paramMainMenu = Constants.ZERO;
          break;
      }
    }
  }
yoppie97
  • 177
  • 4
  • 18
  • Use `NavigationDrawer`, `BottomNavigation` or `ViewPager`. – Zwal Pyae Kyaw Nov 13 '18 at 04:23
  • using NavigationDrawer @ZwalPyaeKyaw – yoppie97 Nov 13 '18 at 04:25
  • 1
    You can use `FragmentManager()` to switch between `Fragment`s. – Zwal Pyae Kyaw Nov 13 '18 at 04:28
  • `getSupportFragmentManager().beginTransaction(). replace(R.id.flContainer, new DemoFragment(), "SOMETAG"). commit(); // Now later we can lookup the fragment by tag DemoFragment fragmentDemo = (DemoFragment) getSupportFragmentManager().findFragmentByTag("SOMETAG");` like this. Here's the [link](https://guides.codepath.com/android/creating-and-using-fragments) to tutorial. – Zwal Pyae Kyaw Nov 13 '18 at 04:33
  • I've use that, the function is the same with clickListener on NavigationDrawer menu. now the problem is when I call the function of switch fragment into handleIntent on Applink, there is no effect. I'm always goto fragment A. – yoppie97 Nov 13 '18 at 04:35
  • Can you add the code to Question? – Zwal Pyae Kyaw Nov 13 '18 at 04:38

2 Answers2

1

your URL should contains some key-values to determine the the fragment to be open let us take a example : Fragment-A is design to show list of offers Fragment-B is design to show list of product

your URL contains a key that should show the "product".

you can segregate the fragment based on the key-value of deeplink

Example

your url is

"https://xyzcompany.com/myapp?open=products"

insinde MainActivity.class onCreate()

String key;
    Intent appLinkIntent = getIntent();
    String appLinkAction = appLinkIntent.getAction();
    Uri appLinkData = appLinkIntent.getData();
    if (appLinkData != null) {
        key = appLinkData.getQueryParameter("open");
    }

 if(key == products){
       //launch Fragment-B
    } else {
      //launch Fraagment-A
 }
Chethan Kumar
  • 185
  • 1
  • 12
0

Android added Navigation Components in Android Jetpack libraries to handle complex navigation scenarios in the application.

You can create destination(Fragments etc.) and link them via actions to navigate from one destination to another destination. You can associate deep links with fragments using navigation components. This should be able solve you problem.

Check the following links for more details:

https://developer.android.com/guide/navigation

https://developer.android.com/guide/navigation/navigation-deep-link

saurabh1489
  • 324
  • 1
  • 6