java - Android Fragment参数包:当键存在时getString返回null
问题描述
我非常有信心我在这里遗漏了一些简单的东西。提前道歉,我是Android新手。
到目前为止,我的应用程序向 Strava OAuth 服务器发送了一个请求,该服务器通过我在请求中使用的回调 URI 返回到我的应用程序的隐式深层链接。我正在尝试获取与传递给片段的回调 URI 一起传递的“代码”和“范围”参数。收到的参数就是这个,通过 getArguments 找到。
Bundle[{android-support-nav:controller:deepLinkIntent=Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=myapp://localhost/fibonacci/itemFragment?state=&code=d37efb9e5b66325ddd34614c41550dd1c00d8444&scope=read,activity:read_all,read_all flg=0x1440c000 cmp=com.example.fibonacci/.MainActivity (has extras) }}]
当我在调试时检查 bundle 变量时,我还看到了一个包含以下内容的层次结构:
bundle -> mMap -> value[0] -> mData -> uriString
uriString 是从 Fragment 的参数派生的整个捆绑包的子集:
myapp://localhost/fibonacci/itemFragment?state=&code=d37efb9e5b66325ddd34614c41550dd1c00d8444&scope=read,activity:read_all,read_all
我一直在尝试的代码如下(参见 onCreate() )。我显然是以一种过于幼稚的方式做某事,因为它为我试图从参数包中取出的东西返回 null 。如果我忽略了 Android 文档中的某个页面,我将不胜感激链接或建议。
谢谢!
package com.example.fibonacci;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.fibonacci.dummy.DummyContent;
/**
* A fragment representing a list of Items.
*/
public class ItemFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
private static final String ARG_STRAVA_CODE = "code";
private static final String ARG_STRAVA_SCOPE = "scope";
private static final String ARG_STRAVA_URISTRING = "uriString";
// TODO: Customize parameters
private int mColumnCount = 1;
private String mCode = null;
private String mScope = null;
private String mUriString = null;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ItemFragment newInstance(int columnCount) {
ItemFragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
Bundle bundle = getArguments();
mUriString = bundle.getString(ARG_STRAVA_URISTRING);
mCode = bundle.getString(ARG_STRAVA_CODE);
mScope = bundle.getString(ARG_STRAVA_SCOPE);
}
// GET the refresh and short-lived access token
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS));
}
return view;
}
}
编辑:我也不明白为什么 'column-count' 参数没有出现在包中。那个是由我选择的 Fragment 模板自动创建的,但我希望它包含在整个捆绑包中。
解决方案
我缺少的是 Bundle 里面有一个完整的 Intent 对象。我必须使用以下代码从片段的 Activity 中查询 Intent:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
Bundle bundle = getArguments();
Intent intent = getActivity().getIntent();
Uri data = intent.getData();
mCode = data.getQueryParameter(ARG_STRAVA_CODE);
mScope = data.getQueryParameter(ARG_STRAVA_SCOPE);
}
// GET the refresh and short-lived access token
}
推荐阅读
- javascript - tensorflowjs - javascript 中是否有等效的标记器方法?
- python - 有没有更好的方法在 python 项目中设置 sys.path.append ?
- r - Markdown PDF Knitr - 减号(U+2212)错误
- typescript - 为什么 Typescript 中常量的类型是 2.8?
- html - 如何使用 CSS 为 SVG 设置动画?
- python - Python findall \n 与正则表达式
- azure - 将 Azure Databricks 日志传输到 azure 日志监视器的平均延迟是多少?
- graphql-ruby - How to return a Union type from a GraphQL Mutation
- c# - C# AlphaVantage.NET: How-to use Proxy
- spring - Cannot get value from properties using @Value in Spring