该应用程序有一个Activity(MainActiviy.java)+片段A,B,C,D等.... Sun在Activity +应用程序中的片段A,想要更改片段B.但是shift片段在片段A中。到目前为止,我们总是直接在Activity按钮中使用并且一切正常。但片段A和B之间的交换在片段A中有直接按钮,不知道如何。特别是使用bundle从A发送到B的putString。
代码按钮片段A
{
@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:
TorrentPlayerFragmentRemote fragment6 = new TorrentPlayerFragmentRemote();
Bundle bundle = new Bundle();
bundle.putString("url", description);
fragment6.setArguments(bundle);
fragment6.onDestroyView();
FragmentTransaction fragmentTransaction6 = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction6.addToBackStack(null);
getFragmentManager().popBackStack();
fragmentTransaction6.remove(fragment6);
fragmentTransaction6.replace(R.id.frame,fragment6);
fragmentTransaction6.commit();
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://dddd.ddd/remote/1.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;
}
}
});
builder.create().show();
}
});
&#34;框架&#34;在活动中。
我认为我们应该更改它以接收MainActivity.java以进行更改。
答案 0 :(得分:0)
如果我理解您正在尝试正确执行的操作,则可以将您的活动传递给Fragment A的构造函数并将其保存。然后在您的活动上创建方法以使用以下内容切换片段:
2.2.1 :125 > remap 'john smith'
=> "wbua fzvgu"
2.2.1 :126 > remap 'i love you!'
=> "v ybir lbh!"
然后在FragmentA中点击你可以在FragmentA的构造函数中调用你保存的活动,并在活动上调用switchFragment函数。