A.1. Send data from This Fragment (Source Fragment)
public class testList extends Fragment {
//Define Event Listener
private TestFragmentEvent listener;public interface TestFragmentEvent {
public void TestFragmentEvent_Create();
public void TestFragmentEvent_View(String data);}public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof SecondFragment.onFrgmtSecondBtnSelected) {
listener = (TestFragmentEvent) context;
} else {
throw new ClassCastException(context.toString() + " must implement listener");
}}
public void f_showData() {String rdata = "{Id:101,Name:"XYZabc"}";
try {
rdata = testDataJsonArray.getJSONObject(i).toString();
} catch (JSONException e) {
e.printStackTrace();
}
listener.TestFragmentEvent_View(rdata);}}
A.2. In Main Activity
public class MainActivity extends AppCompatActivity implements testList.TestFragmentEvent {@Override
public void TestFragmentEvent_View(String data) {
testView testview = new testView();
Bundle bundle = new Bundle();
bundle.putString("rdata", data);
testview.setArguments(bundle);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment, testview);
fragmentTransaction.commit();}}
A.3. Get data on Load Fragment (Destination Fragment)
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test_view, container, false);
// Coding here
tvId = view.findViewById(R.id.tv_test_id);
tvName = view.findViewById(R.id.tv_test_name);
String data =this.getArguments().getString(MSG);
Bundle bundle = this.getArguments();
String data = bundle.getString("rdata");
JSONObject jsonObject;
try {
jsonObject = new JSONObject(data);
//fill data
tvName.setText(jsonObject.getString("name"));
tvId.setText(jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
// Coding here
return view;
}
B.1. Send data from This Fragment (Source Fragment)
public class testList extends Fragment {
//Define Event Listener
private TestFragmentEvent listener;public interface TestFragmentEvent {
public void TestFragmentEvent_Create();
public void TestFragmentEvent_View(String data);}public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof SecondFragment.onFrgmtSecondBtnSelected) {
listener = (TestFragmentEvent) context;
} else {
throw new ClassCastException(context.toString() + " must implement listener");
}}
public void f_showData() {String rdata = "{Id:101,Name:"XYZabc"}";
try {
rdata = testDataJsonArray.getJSONObject(i).toString();
} catch (JSONException e) {
e.printStackTrace();
}
listener.TestFragmentEvent_View(rdata);}}
B.2 Main Activity
public class MainActivity extends AppCompatActivity implements testList.TestFragmentEvent {@Override
public void TestFragmentEvent_View(String data) {
testView testview = new testView(data);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment, testview);
fragmentTransaction.commit();}}
B.3. Get data on Load Fragment (Destination Fragment)
public class testView extends Fragment {
String rdata;
public testView(){
}
public testView(String data){
rdata = data;
}
TextView tvId, tvName;
//traverse variable
public static String MSG = "com.omgrk.OMGRKMngTestAndroid.Test.MSG";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test_view, container, false);
// Coding here
tvId = view.findViewById(R.id.tv_test_id);
tvName = view.findViewById(R.id.tv_test_name);
JSONObject jsonObject;
String data = rdata;
try {
jsonObject = new JSONObject(data);
//fill data
tvName.setText(jsonObject.getString("name"));
tvId.setText(jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
// Coding here
return view;
}
}
No comments:
Post a Comment