深度链接到另一个片段[Android]

活动和片段的结构如下:

使用NavigationDrayer。

MainActivity有一个碎片容器,一个容器中应该有多个碎片。应用启动后,默认分片为分片A

使用片段B实现深度链接到MainActivity,以及如何将片段从片段A(默认)更改为片段B(目标)。

任何帮助都会有帮助:) 谢谢

即 类句柄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)

执行函数

    @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;
      }
    }
  }

URL

您的推荐答案应包含一些键值,以确定要打开的片段 让我们举一个例子: 片段A旨在显示报价列表 片段B旨在显示产品列表

您的URL包含应显示"产品"的密钥。

您可以根据deeplink的键值进行分片

示例

您的URL是

"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
 }

相关文章