SAYAM.S SANGHVI
SAYAM.S SANGHVI

Reputation: 39

How to send data from an Activity to a custom dialog which extends Dialog Fragment

I am a beginner in android studio. I am stuck at this point of not able to pass data from activity to dialog.I have tried bundle but that is also not working cause it shows NULL point exception.Or i guess my method is wrong.It would be great if i could get some help.

Dialog class

public class Mdlog extends DialogFragment
{
LayoutInflater inflater;
TextView sc1,sc2;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
    inflater=getActivity().getLayoutInflater();
    View view=inflater.inflate(R.layout.layout_dialog,null);
    Intent i=getActivity().getIntent();
    sc1= (TextView) view.findViewById(R.id.score1);
    sc2= (TextView) view.findViewById(R.id.score2);
    sc1.setText(i.getStringExtra("sc1"));
    sc2.setText(i.getStringExtra("sc2"));
    builder.setView(view);
    AlertDialog dialog=builder.create();

    return dialog;
}}

MainActivity class

 public class MainActivity extends AppCompatActivity {

int sc1=0;
int sc2=0;

TextView t;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t= (TextView) findViewById(R.id.timeleft);
    new CountDownTimer(3000,1000)
    {

        @Override
        public void onTick(long millisUntilFinished)
        {
            t.setText(""+millisUntilFinished/1000);
            sc1+=1;
            sc2+=2;
        }

        @Override
        public void onFinish()
        {

        }
    }.start();
    Intent i=new Intent(this,Mdlog.class);
    i.putExtra("sc1",sc1);
    i.putExtra("sc2",sc2);
    startActivity(i);
    Mdlog dialog=new Mdlog();
    dialog.show(getFragmentManager(),"sss");

}}

Upvotes: 1

Views: 2926

Answers (2)

AtaerCaner
AtaerCaner

Reputation: 712

Delete whole,

Intent i=new Intent(this,Mdlog.class);
    i.putExtra("sc1",sc1);
    i.putExtra("sc2",sc2);
    startActivity(i);

Set a bundle for arguments to pass a data then set to your fragment,

    Bundle args = new Bundle();
    args.putInt("sc1", sc1);
    args.putInt("sc2", sc2);
    Mdlog dialog=new Mdlog();
    dialog.setArguments(args);
    dialog.show(getFragmentManager(),"dialog");

And get data in your dialog with,

Bundle args = getArguments();
int sc1 = args.getInt("sc1");
int sc2 = args.getInt("sc2");

Upvotes: 4

csabapap
csabapap

Reputation: 1031

You should create your fragment with a static method. If you type newIsntance in your DialogFragment Android Studio will generate you a newInstance static method. You can pass your data to that method. For example:

public static DialogFragment newInstance(int value1, int value2) {
    DownloadDialogFragment dialogFragment = new DownloadDialogFragment();

    Bundle args = new Bundle();
    args.putInt("args_value1", value1);
    args.putInt("args_value2", value2);

    dialogFragment.setArguments(args);
    return dialogFragment;
}

You can get your data in onCreateDialog(Bundle savedInstanceState) like this:

public Dialog onCreateDialog(Bundle savedInstanceState) {

    int value1 = getArguments().getInt("args_value1");
    int value2 = getArguments().getInt("args_value2");
}

Upvotes: 0

Related Questions