Android Fragment - Application Crashed after Closing Application - android

In my application I am using google map in fragment. I am using this code for adding fragments.
public void addPage(final BaseFragment pBaseFragment, final boolean isAddToBackStack){
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.add(R.id.content_frame, pBaseFragment);
if (isAddToBackStack) transaction.addToBackStack(null);
transaction.commit();
}
In MyMapFragment I call this for removing map (If I don't do this I got duplicate id for map exception)
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_n));
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
but when I press back button and application is closed I got this error. (in the line ft.commit())
Caused by: java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
Could anyone tell me what is the solution here how avoid this error I've already stuck on this 3 days.

Try this way:
#Override
public void onDestroyView ()
{
try{
SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_n));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}catch(Exception e){
}
super.onDestroyView();
}

Related

How do I handle a custom scheme from a Fragement in Android?

I'm working with an Android app which I inherited. On a certain screen, which is defined as a Fragment, I need to open an external web page, and then wait for that web page to do a redirect back to my app using a custom scheme.
I understand how to open the web page, and I understand how to set up an intent-filter in my Manifest that responds to the custom scheme by starting another activity. But, starting another activity is not what I need. Instead, I need for control to return to the Fragment that originally started this process.
What is the best way to accomplish this?
You have to parse the intent in the activity and use the fragment manager to populate late the fragment you wish. Replace Action and Fragment with your own.
#Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
parseIntent(intent);
}
private void parseIntent(Intent intent) {
final String action = intent.getAction();
if (action != null) {
if (Action.<ONE>.equals(action)) {
FragmentManager fm = getFragmentManager();
Fragment<ONE> fragment = (Fragment<ONE>) Fragment.instantiate(this,
Fragment<ONE>.class.getCanonicalName(),
getIntent().getExtras());
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_id, fragment);
ft.commit();
} else if (Action.<TWO>.equals(action)) {
FragmentManager fm = getFragmentManager();
Fragment<TWO> fragment = (Fragment<TWO>) Fragment.instantiate(this,
Fragment<TWO>.class.getCanonicalName(),
getIntent().getExtras());
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_id, fragment);
ft.commit();
}
}
}

zxing-android-embedded: Load new Fragment from continous scan

I´ve the following problem: I want to load a new Fragment from my QR Activity. I start the activity from my fragment like this:
IntentIntegrator.forSupportFragment(this).setCaptureActivity(QrHorizontalActivity.class).initiateScan();
Then in the scan activity I want to load my new fragment:
#Override
public void barcodeResult(BarcodeResult result) {
SwipeOverviewFragment.newInstance().add_fragment(R.id.barcode_scanner,curr_activity, detail_frag);
Why will this not work? Thanks a lot for your help
Try below code for calling fragment;
FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
SwipeOverviewFragment regcomplainfragment = new SwipeOverviewFragment();
fragmenttransaction.replace(R.id.content_frame, regcomplainfragment).addToBackStack("tag");
fragmenttransaction.commit();

Android prevent DialogFragment opening twice

I am trying to prevent my DialogFragment opening twice. Here is what I do:
I try to keep only one instance of my fragment. I create and add my fragment like this:
//MyFragment.java
public static MyFragment mInstance;
public static void instantiateFragment() {
MyFragment myFragment = MyFragment.getInstance();
if(!myFragment.isAdded()) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(myFragment, TAG);
ft.commit();
}
}
private static MyFragment getInstance() {
if(mInstance == null) {
mInstance = new MyFragment();
}
return mInstance;
}
And when a button is clicked, I intentionally try to add fragment twice like this:
MyFragment.instantiateFragment();
MyFragment.instantiateFragment();
But I get IllegalStateException: Fragment already added. Any ideas about that?
Thanks.
Indeed it's a problem with asynchronous commit of transactions, so as #Android jack stated you can use executePendingTransactions() like in this answer,
or even better use commitNow(),
or try something like this:
public static void instantiateFragment() {
Fragment myFragment = getSupportFragmentManager().findFragmentByTag(TAG);
if (myFragment == null) {
myFragment = MyFragment.getInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(myFragment, TAG);
ft.commit();
}
}
I think this has to do with the asynchronous behaviour of fragment transactions.Fragment Transactions are committed asynchronously. So at first call, your fragment is added but it is committed asynchronously.Again in your next call your fragment is not added as it is not committed yet so !myFragment.isAdded() returns false.Then while adding the fragment the previous transaction is committed due to which it raises exception.
Try to use this
getFragmentManager().executePendingTransactions();
before your (!myFragment.isAdded()) code.

When Activity is destroyed on background,how to remove fragment

When my app switches to background,the system memory is low, then android will destroy my activity.At that time,I want to remove the fragments attached to activity,so that when activity switches to foreground,I can avoid any abnormal behavior of that activity.I do it like this:
#Override
public void onSaveInstanceState(Bundle outState) {
FragmentManager fm = getSupportFragmentManager();
AbstractBaseViewFragment previous = (AbstractBaseViewFragment) fm.findFragmentByTag(FRAGMENT_TAG);
if (previous != null) {
fm.beginTransaction().remove(previous).commitAllowingStateLoss();
fm.executePendingTransactions();
}
super.onSaveInstanceState(outState);
}
so I want to ask:Are there any other good ideas to finish this target?Will my method cause any crashes?
You can try this: ft.detach(previous);
if (previous != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.detach(previous);
ft.remove(previous);
ft.commitAllowingStateLoss();
... // the rest of your code
}

Why addToBackStack() method doesn't work

Here is my main activity. I followed this guide about Fragments correctly. When I click "Back" button, my application is closed instead of returning to the MainScreenFragment. Why is this happening and why addToBackStack() doesn't work?
public class MainScreenActivity extends ActionBarActivity implements MainScreenFragment.OnFrameChoiced {
private MainScreenFragment mainScreenFragment;
private AddWordsFragment addWordsFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
mainScreenFragment = new MainScreenFragment();
addWordsFragment = new AddWordsFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, mainScreenFragment).addToBackStack(null).commit();
}
#Override
public void choiceFrame(int id) {
switch (id) {
case R.id.add_new_words_frame:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, addWordsFragment).addToBackStack(null).commit();
fm.executePendingTransactions();
break;
}
}
P.S. I tried to use a solution from this topic, but It still doesn't work.
did you try overriding the back like below:
#overide
public void onBackPressed(){
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 1) {
fm.popBackStack();
} else {
finish();
}
}
(I know you have picked up your desirable answer, but I have found a little more against this problem)
Though as Android official site has documented:
By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Backbutton.
But as a matter of fact, this is in a precondition that you are using the standard android activity, specifically, the android.app.Activity. Because this methods in android.app.Activity will work when Backbutton is pressed:
public void onBackPressed() {
if (mActionBar != null && mActionBar.collapseActionView()) {
return;
}
if (!mFragments.getFragmentManager().popBackStackImmediate()) {
finishAfterTransition();
}
}
But, if you are extending your xxxxActivity from someone else, for example, the AppCompatActivity, FragmentActivity, ActionBarActivity, it will be another story, because in FragmentActivity, onBackPressed() method is totally overrode:
public void onBackPressed() {
if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) {
supportFinishAfterTransition();
}
}
Note that mFragments.getFragmentManager() is replaced by mFragments.getSupportFragmentManager(), so in cases like this, you should begin your FragmentTransaction using getSupportFragmentManager() of the Activity. and as a consequence of that, you don't have to override onBackPressed method in your Activity.
BTW, ActionBarActivity extends AppCompatActivity extends FragmentActivity,they all come from the support library, you know what I mean, remember to use getSupportFragmentManager() instead of getFragmentManager() when using support library in order to get the compatible behavior.

Categories

Resources