Reputation: 1972
In my application I have a dialog fragment which opens first before launching of MainActivity. When some data is entered in that dialog fragment and click confirmed I need to send some value (means a double value) to a fragment(Main).
My main activity consists of view pager which has two fragments. one is fragment(Main).. i have to send the data to my dialog fragment to the fragment(main)
The fragment(Main) is attached to MainActivity by viewpager. I have tried by sending the value to fragment(Main) by using Intents and bundle. referred the sites to pass data between fragments.
while by using Intents it showing the exception unable to explicit have you declared in android Manifest
while by trying with bundle it getting exception bundle is null.
from these two exceptions I have got is it possible to send the data.. can any body help and give suggestion.
here is my dialog fragment:
public class InputFragment extends DialogFragment {
public InputFragment() {
// Required empty public constructor
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.input, container, false);
final EditText ed = (EditText)view.findViewById(R.id.editText);
final RadioButton rb1 = (RadioButton)view.findViewById(R.id.radioButton1);
final RadioButton rb2 = (RadioButton)view.findViewById(R.id.radioButton2);
final Button b1 = (Button)view.findViewById(R.id.cancel);
final Button b2 = (Button)view.findViewById(R.id.confirm);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!ed.getText().toString().equals("")) {
if (rb1.isChecked()) {
int input = Integer.valueOf(ed.getText().toString());
double out = input / 24.0;
out = Double.parseDouble(new DecimalFormat("#.0").format(out));
Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);
m.putExtra("res", out);
getActivity().startActivity(m);
} else if (rb2.isChecked()) {
int input = Integer.valueOf(ed.getText().toString());
double out = ((input * 2 / 3) * 0.029574);
out = Double.parseDouble(new DecimalFormat("#.0").format(out));
Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);
m.putExtra("res", out);
getActivity().startActivity(m);
}
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
fragment that receive data (Main):
public class MainScreen extends Fragment {
double ram;
TextView gls;
MainScreen mainScreen;
public MainScreen() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_screen, container, false);
mainScreen = this;
TextView textView = (TextView)view.findViewById(R.id.res);
Intent r = getActivity().getIntent();
double res = r.getDoubleExtra("res", 0);
String result = Double.toString(res);
textView.setText(result);
ram = Double.parseDouble(textView.getText().toString());
gls = (TextView) view.findViewById(R.id.glasses);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DiaFragment df = new DiaFragment();
df.setCallingFragment(mainScreen);
df.show(getFragmentManager(),"Frag");
}
});
return view;
}
Above I tried by intent,
by using bundle: in my fragment(Main)
Bundle mbundle = mainScreen.getArguments();
double res = mbundle.getDouble("res",0);
String result = Double.toString(res);
textView.setText(result);
dialog fragment:
InputFragment inputFragment = new InputFragment();
Bundle bundle = new Bundle();
bundle.putDouble("res",out);
inputFragment.setArguments(bundle);
Upvotes: 1
Views: 1042
Reputation: 1517
1.create interface :
public interface OnReceivedData {
public void onDataReceived(Objects objects);
}
2.create Dialog :
public class Dialog extends DialogFragment {
private OnReceivedData mData;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mData=(OnReceivedData) activity;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// create dialog
senDataToActivity();
}
private void senDataToActivity() {
mData.onDataReceived("your object");
}
}
3.call dialog :
public class MainActivity extends AppCompatActivity implements OnReceivedData {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void opneDialog(){
//
}
@Override
public void onDataReceived(Objects objects) {
// receded data
// do something
}
}
Upvotes: 1
Reputation: 2339
You cannot call a fragment like this:
Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);
Instead this:
MainScreen fragment = new MainScreen();
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(id, fragment , "MainScreen");
ft.addToBackStack("");
ft.commit();
Upvotes: 1