Reputation: 947
I have seen numerous similar questions on stackoverflow but I can not figure out what I am doing wrong. I have made a very simple app to replicate the error.
The app does the following:
it adds a fragment in the MainActivty's onCreate.
user can add another fragment from a dialog.
when the dialog calls the MainActivity's addFragment method an exception is thrown on the commit of the transaction.
Here is the MainActivity
package com.example.testapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.add_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddButtonDialog addButtonDialog = new AddButtonDialog();
addButtonDialog.show(getSupportFragmentManager(), "ADD");
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
CustomFragment fragment = new CustomFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
}
public void addButton() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
CustomFragment fragment = new CustomFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit(); // <-- Exception
}
}
and here is the AddButtonDialog that calls the MainActivity
public class AddButtonDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.new_button_dialog, null);
builder.setView(view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
MainActivity main = new MainActivity();
main.addButton();
}
});
return builder.create();
}
}
And here is the stacktrace
02-14 10:09:27.781: E/AndroidRuntime(26291): FATAL EXCEPTION: main
02-14 10:09:27.781: E/AndroidRuntime(26291): java.lang.IllegalStateException: Activity has been destroyed
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.example.testapp.MainActivity.addButton(MainActivity.java:35)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.example.testapp.AddButtonDialog$1.onClick(AddButtonDialog.java:22)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.os.Looper.loop(Looper.java:137)
02-14 10:09:27.781: E/AndroidRuntime(26291): at android.app.ActivityThread.main(ActivityThread.java:5328)
02-14 10:09:27.781: E/AndroidRuntime(26291): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 10:09:27.781: E/AndroidRuntime(26291): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-14 10:09:27.781: E/AndroidRuntime(26291): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-14 10:09:27.781: E/AndroidRuntime(26291): at dalvik.system.NativeStart.main(Native Method)
I am fairly certain I am not grasping the concepts of the Fragment lifecycle. Please would someone be so kind as to explain what I am doing wrong.
Upvotes: 0
Views: 1571
Reputation: 43394
Pass a reference of MainActivity
to the Dialog and try this
mainActivity.addButton()
Where mainActivity
is the reference
Upvotes: 1
Reputation: 272
If all you want to do is calling a method in your Activity from somewhere else:
1 - create a static variable in your main activity, of type Activity, and instantiate it in onCreate to this, so you have your instance stored.
2 - create a static method in your main activity called get instance, that simply returns an instance of your activity
3 - wherever you are call : MainActivity.getInstance().yourMethod();
Of course it will only work if your activity is existing!
Upvotes: 0
Reputation: 6792
If your AddButtonDialog is an inner class then simply make a call to addActivity rather than doing main.addButton().
If not then try creating this method in a separate class and pass FragmentManager as a parameter to it. Thus you can call this from anywhere creating instance of the newly made class.
Also, try using the below in your addButton method,
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
CustomFragment fragment = new CustomFragment();
transaction.replace(R.id.fragment_container, fragment); //this will replace your fragment_container with this fragment
transaction.addToBackStack(null);// this will help you get back to your first fragment on back press
transaction.commit();
Upvotes: 0
Reputation: 11321
don't:
MainActivity main = new MainActivity();
main.addButton();
the way to start Activities in Android is via startActivity(intent); method
So for example do instead:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
see Intent Guide
Upvotes: 0