Debojyoti
Debojyoti

Reputation: 169

Cant get getSupportFragmentManager from activity and the error is activity is being destroyed

My fragment class is

public class GetFragmentManager extends FragmentActivity {
    public FragmentManager getSupportFragmentMethod(){
        FragmentManager s = getSupportFragmentManager();
        return s;
    }
}

I needed the getSupportFragmentManager method(which i can get from the FragmentActivity class) so i made this class which extends the FragmentActivity class.

My Activity code(this extends the Activity class) is

public void showFileChooser(View v){
    DialogFragment a =new FireMissilesDialogFragment();
    a.show(getSupportFragmentManager1(), "missiles");

}

private android.support.v4.app.FragmentManager getSupportFragmentManager1() {
     android.support.v4.app.FragmentManager ab = new GetFragmentManager().getSupportFragmentMethod();
    return ab;
}

The error statement coming is Activity is being destroyed.

Please can anyone find what is going wrong in here.I have spend many hours on this.Thanks everyone for your time.

Upvotes: 1

Views: 3024

Answers (4)

Daniel Nyamasyo
Daniel Nyamasyo

Reputation: 2312

just extend AppCompatActivity instead of Activity and then you can use

FireMissilesDialogFragment a =new FireMissilesDialogFragment();
    a.show(getSupportFragmentManager1(), "missiles");

Upvotes: 0

Debojyoti
Debojyoti

Reputation: 169

Sorry for wasting all of your time.
It seems that Activity class has a method getFragmentManager(), which I knew at the time, but wasn't running correctly, as I was wrong in referencing a class (specifically the fragment class) in a code line. Also at every import on fragments I imported not the app.v4 support version but the main version.

The main activity class extends the Activity class

public class Profile extends Activity implements View.OnTouchListener{

And on the same class the open fragment method is written

public void showFileChooser(View v){

    a =new FireMissilesDialogFragment();
    a.show(getFragmentManager(),"text");

}

and the FireMissilesDialogFragment is as follow.

public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("dialog_fire_missiles")
            .setPositiveButton("fire", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}
}

The things which changed are that for every app.v4 support version import i didn't import the v4 version but the regular version. Even if the dialog works the manifest file will show the error "java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity"

Anyways thanks everyone for your time. I will upvote the two answers as they have been useful in coming at this point.

Upvotes: 2

Andrei T
Andrei T

Reputation: 3083

First of all, if you want to use activity for starting a fragment you first need to pass the onCreate threshold, then you create the fragment:

public class MyActivity  extends AppCompatActivity {
  @Override
  public void onCreate(Bundle saveInstanceState){
      super.oncCreate(saveInstanceState);
      //create your file chooser, etc.
      DialogFragment a =new FireMissilesDialogFragment();
      a.show(getSupportFragmentManager(), "missiles"); 
       //getSupportManager exists in the activity
  }
}

You can also create it in onResume, onStart, whenever you feel like.

Edit

For appcompat you can look at the following to understand the issue:
Update your style resources
For relevant stack posts:
You need to use a Theme.AppCompat theme (or descendant) with this activity
As you can understand you need to define the theme in your activity or in your application:

android:theme="@style/Theme.AppCompat" >

Upvotes: 1

dumb_terminal
dumb_terminal

Reputation: 1825

you can not instantiate an Activity like you instantiated new GetFragmentManager() , pass an already instantiated activity to the method.

For example your main activity (the activity currently on screen) extends fragment manager, then inside that you call this.getSupportFragmentManager()

Upvotes: 1

Related Questions