我从片段A调用活动方法。工作正常。 但是我如何设置一个字符串,然后打开片段B并从字符串中接收信息?
的活动:
public void test(){
Send_torrent_url_androidTv_Fragment fragment4 = new Send_torrent_url_androidTv_Fragment();
fragment4.onDestroyView();
FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
fragmentTransaction4.addToBackStack(null);
getFragmentManager().popBackStack();
fragmentTransaction4.remove(fragment4);
fragmentTransaction4.replace(R.id.frame,fragment4);
fragmentTransaction4.commit();
}
片段A:
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
// getting values from selected ListItem
final String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
final String id_stream = ((TextView) view.findViewById(R.id.id_streaming)).getText().toString();
// Starting single contact activity
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
builder.setTitle("Select");
// builder.setMessage("Lorem ipsum dolor ....");
builder.setItems(new CharSequence[]
{getString(R.string.play_video), getString(R.string.remove_video)},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
Activity activity = getActivity();
if(activity instanceof Main2Activity){
Main2Activity myactivity = (Main2Activity) activity;
myactivity.test();
}
break;
case 1:
// Snack Bar
Snackbar bar = Snackbar.make(view, R.string.confirm_delete_playlist, Snackbar.LENGTH_LONG)
.setAction(R.string.yes, new View.OnClickListener() {
@Override
public void onClick(View v) {
// ---Control remote api---
new Thread() {
public void run() {
try {
HttpURLConnection.setFollowRedirects(false);
// note : you may also need
//HttpURLConnection.setInstanceFollowRedirects(false)
HttpURLConnection con = (HttpURLConnection) new URL("http://xxxx/remote/delete-.php?id="+id_stream).openConnection();
con.setRequestMethod("HEAD");
if(con.getResponseCode() == HttpURLConnection.HTTP_OK) {
//--refresh fragment
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(playlist_torrent.this).attach(playlist_torrent.this).commit();
//Fin refresh fragment
// startActivity(getIntent());
// finish();
/* final Handler handler = new Handler();
Runnable refresh = new Runnable() {
@Override
public void run() {
new onPreExecute().execute();
handler.postDelayed(this, 60 * 1000);
}
};
handler.postDelayed(refresh, 60 * 1000); */
}
else{
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}.start();
// ----fin Control remote api----
}
});
bar.show();
break;
}
}
});
到目前为止,一切进展顺利。但现在放置一个字符串包?
Send_torrent_url_androidTv_Fragment fragment4 = new Send_torrent_url_androidTv_Fragment();
> Bundle bundle = new Bundle();
> bundle.putString("token", description);
> fragment4.setArguments(bundle);
fragment4.onDestroyView();
FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
fragmentTransaction4.addToBackStack(null);
getFragmentManager().popBackStack();
fragmentTransaction4.remove(fragment4);
fragmentTransaction4.replace(R.id.frame,fragment4);
fragmentTransaction4.commit();
当您输入“test”时,“description”是一段片段A而不起作用 片段b:
Bundle args = getArguments();
String token = args.getString("token");
(错误)。你能做什么?谢谢
答案 0 :(得分:0)
使用newInstance
创建片段,它将用作带参数的构造函数。你可以找到很多samples in the documentation。
在您的片段类中,创建方法
public static Send_torrent_url_androidTv_Fragment newInstance(String param) {
Send_torrent_url_androidTv_Fragment fragment = new Send_torrent_url_androidTv_Fragment();
Bundle args = new Bundle();
args.putString("somelabel", param);
fragment.setArguments(args);
return fragment;
}
在片段类的每个部分中,您都可以使用
访问此值this.getArguments().getString("somelabel")
然后,您可以使用activity.test("bazinga")
将值传递给片段,其中test就是这样的
public void test(String param){
Send_torrent_url_androidTv_Fragment fragment4 = Send_torrent_url_androidTv_Fragment.newInstance(param);
//... more code...
}