GetActivity () is null inside the AlertDialog fragment

Why getActivity() return null inside an AlertDialog ?

This is a class -

 Class A extends Common{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button save = (Button) view.findViewById(R.id.save); save.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("the activity outside dialog.."+getActivity()); AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); alert.setPositiveButton("Check acitivity", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); System.out.println("the activity inside dialog.."+getActivity()); } }); } } 

The general class extends the fragment as

 import android.support.v4.app.Fragment; Class Common extends Fragment { //Some code } 

And the conclusion is

 the activity outside dialog..com.testapp.main.MainActivity@42131080 the activity inside dialog..null 
+6
source share
7 answers
  Activity activity; @Override public void onAttach(Activity activity) { super.onAttach(activity); this.activity=activity; } 
+6
source

I think getActivity () inside DialogInterface points to the dialog context

 dialog.getActivity() 

inside your dialog, try changing it to:

 A.this.getActivity(); 

Edit : I checked getActivity () inside DialogInterface and it should work fine.

another solution might use the onAttach Callback function and get your activity. Context to make sure your fragment is tied to parent activity before using it in the dialog box. then use it instead of getActivity ().

 @Override public void onAttach(Activity activity) { super.onAttach(activity); yourActivity = activity; } 
+4
source
 Why the getActivity() is returning null inside AlertDialog ? 

This is because you are calling getActivity () DialogInterface.OnClickListener and not your fragment, thereby giving a null value.

Decision:

 A.this.getActivity(); 

you need to call your class reference with this and call getActivity() method

EDIT:

 @Override public void onClick(View v) { System.out.println("the activity outside dialog.."+getActivity()); AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); alert.setPositiveButton("Check acitivity", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { System.out.println("the activity inside dialog.."+ A.this.getActivity()); dialog.cancel(); } }); 
+1
source

replace getActivity () with A.this

 AlertDialog.Builder alert = new AlertDialog.Builder(A.this); 
+1
source

getActivity() returns null until the fragment is attached to the activity.

you intend to create an object after calling the onAttach method.

you can create an object in the onCreateview method for best practices.

0
source

Based on the answers of Sudhi S and Arash, I think this is a cleaner option that deals with the action locally for the method calling it:

 private void showDialog() { final Activity activity = getActivity(); AlertDialog.Builder builder = new AlertDialog.Builder(activity) .setTitle(R.string.my_title) .setMessage(R.string.my_message) .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.e(TAG, "the activity inside dialog... " + activity); } }); final AlertDialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(true); dialog.show(); } 
0
source
  @Override public void onAttach(Activity activity) { super.onAttach(activity); yourActivity = activity; } 
-1
source

Source: https://habr.com/ru/post/972639/


All Articles