Jay Vyas
Jay Vyas

Reputation: 2712

Call fragment from activity in android

I am current working on project which is in used Fragment. But Here, When i call to activity class From Fragment it's run perfectly. What i have to do is that on Back Pressed i need to call a Fragment.But i can't , it shows me error and my app stops.

So my question here is that how can i call fragment from activity so that my sequence should be fragment>activity>fragment.

07-11 16:22:12.190: E/AndroidRuntime(11963): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

& when i want to call activity from fragment it's give error

07-11 15:52:25.961: E/FragmentManager(11885): No view found for id 0x7f05003c for fragment

So, how can i call fragment from activity & activity from fragment?

Upvotes: 1

Views: 248

Answers (2)

sjain
sjain

Reputation: 23344

  • java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

Solution:

Use transaction.commitAllowingStateLoss(); when adding or performing the FragmentTransaction that was causing the Exception.

 Why was the exception thrown?

The exception was thrown because you attempted to commit a FragmentTransaction after the activity's state had been saved, resulting in a phenomenon known as Activity state loss.

When you call FragmentTransaction#commit() after onSaveInstanceState() is called, the transaction won't be remembered because it was never recorded as part of the Activity's state in the first place. From the user's point of view, the transaction will appear to be lost, resulting in accidental UI state loss. In order to protect the user experience, Android avoids state loss at all costs, and simply throws an IllegalStateException whenever it occurs.

NOTE:

Use commitAllowingStateLoss() only as a last resort. The only difference between calling commit() and commitAllowingStateLoss() is that the latter will not throw an exception if state loss occurs. Usually you don't want to use this method because it implies that there is a possibility that state loss could happen. The better solution, of course, is to write your application so that commit() is guaranteed to be called before the activity's state has been saved, as this will result in a better user experience. Unless the possibility of state loss can't be avoided, commitAllowingStateLoss() should not be used.

More from: fragment-transaction-commit-state-loss.

  • FragmentManager(11885): No view found for id 0x7f05003c for fragment

Solution:

The Fragment manager cannot able to find a view with R.id.Container according to what you set in a layout at setContentView of activity.

So whatever layout you have set in setContentView, that layout doesn't contain that view with that id that resolves to id 0x7f05003c say of R.id.Container.

Upvotes: 1

Simas
Simas

Reputation: 44118

Try changing

transaction.commit();

to

transaction.commitAllowingStateLoss();

Or comment out the super onSaveInstance method on your activity:

@Override
protected void onSaveInstanceState(Bundle outState) {
    //super.onSaveInstanceState(outState);
}

To call activity from a fragment you can use:

((YourActivity)getActivity).someMethod();

Upvotes: 1

Related Questions