Fragment not attached to Activity [How to Solve]

1. About fragment (xxfragment) not attached to activity exception. This exception occurs because when the fragment has not been attached to the activity, it calls functions such as getresource() that need context content. The solution is to write the calling code in onstart(). There are also several such references on the Internet http://stackoverflow.com/questions/10919240/fragment-myfragment-not-attached-to-activity The answer is mainly in the call

getResources().getString(R.string.app_ name);

Before adding a judgment isadded (), two said that the exception solution is http://stackoverflow.com/questions/6870325/android-compatibility-package-fragment-not-attached-to-activity

This is a solution to another situation

2. When using fragment to save parameters, it may be because there are too many or too many parameters to save. In this case, the page will cause an exception. Like code

[java]view plaincopy

Bundle b =newBundle();

b.putParcelable(“bitmap”, bitmap2);

imageRecognitionFragment.setArguments(b);

[java]view plaincopy

Bundleb=newBundle();

b.putParcelable(“bitmap”,bitmap2);

imageRecognitionFragment.setArguments(b);

After setting the parameters and adding the hide(), add(), method, you need to commit() to realize two fragment jumps. In this case, the parameters need to be saved by the system. But at this time, you have realized the jump, but the system parameters have not been saved. It will be reported at this time

java.lang.IllegalStateException:CannotperformthisactionafteronSaveInstanceState

Abnormal. Analysis reason: you don’t need the parameters saved by the system, as long as the parameters set by you can be passed and accepted in another fragment. Now Android provides another form of submission method commitalowingstateloss (). From the name, you can see that this kind of submission allows the state value to be lost. This problem has been solved perfectly, and the value transfer is controlled by you

Let’s talk about another problem here. Bitmap can also be passed through bundles. Just use putparacelable

In addition:

public final boolean isAdded()

If the fragment object is added to its activity, it returns true, otherwise it returns false

 

 

Similar Posts: