[英]Impossible to pass string through activities at the beginning
我正在嘗試將字符串從一個活動傳遞到另一個活動。 我需要此字符串來發送帶有特定鍵(字符串)作為標頭的Http請求。
Intent intent = new Intent(getActivity(), StatusActivity.class).putExtra(Intent.EXTRA_TEXT,"");
intent.putExtra("token", token);
startActivity(intent);
我嘗試在其他活動中獲取價值:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_status, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.mylist);
//
Intent intent = getActivity().getIntent();
token = intent.getExtras().getString("token");
adapter = new ObjAdapter(getActivity() ,new ArrayList<Post>());
listView.setAdapter(adapter);
return rootView;
}
但是問題是我在方法onCreate內初始化了一個ArrayAdapter,因為我希望數據出現在活動開始時。 令牌似乎為空,無法找到。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new StatusFragment())
.commit();
}
FeedAsync task = new FeedAsync();
task.execute();
}
在活動開始時如何獲取字符串令牌? 我需要字符串令牌來執行AsyncTask。
調用super
方法后, Activity
的“最開始”位置是onCreate
方法。 沒有最早可以獲取Intent
數據的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.