-2

my problem is that i am trying to start a fragment from an activity that extends from ListActivity i tryed this solution :

FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment, TAG).commit();

        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment, TAG)
                .addToBackStack(TAG)
                .commit();

the problem is: getSupportFragmentManager() is unknow because my activity doesn't extends from FragmentActivity. i don't want to extend from FragmentActivity is there any solution to do it?

Fakher
  • 2,098
  • 3
  • 29
  • 45

4 Answers4

1

instead of ListActivity use Activity or FragmentActivity or AppCompatActivity its easy solution

if you still want to use than solution is easy to create but hard to maintain.

Just copy the native ListActivity class into your project, rename it "FragmentListActivity", and let it extend FragmentActivity instead of Activity. No other tweaks are required and the copy is easy. If you are uncertain you can peak at my code, but it's always best to derive the latest.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1
  1. extends AppCompatActivity.
  2. After popBackStackImmediate please check the fragment is already present in back-stack or not and then only create new fragment

private void replaceFragment(Fragment fragment) {

String backStateName = fragment.getClass().getName();.

FragmentManager manager = getFragmentManager();

boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

if (!fragmentPopped) { //fragment not in back stack, create it.

    FragmentTransaction ft = manager.beginTransaction();

    ft.replace(R.id.fragment_place, fragment);

    ft.addToBackStack(backStateName);

    ft.commit();

}

}


    compile 'com.android.support:appcompat-v7:22.0.0' // add this dependency 
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Sushil
  • 147
  • 1
  • 9
  • no i don't need to support the 2.x versions, i'm looking for an easier way to do it, because changing the extends to fragmentActivity will effect a huge changes in my code. but i think that it's the only solution !! but i must extend from FragmentActivity!!! – Fakher Aug 11 '15 at 10:56
0

Extend your activity from AppCompatActivity like this: BaseActivity extends AppCompatActivity

this way you add support to your app to older devices, plus the fragment's should be android.support.v4.app.Fragment; and everything will work fine.

Also in your app.gradle add this, so you can use AppCompat library:

compile 'com.android.support:appcompat-v7:22.2.1'

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
0

Based on previous answers:

It seems that you should find another way to show your fragment in ListActivity, for example to show DialogFragment with custom styles or something similar...

IMHO the best solution is to put all the logic from ListActivity to ListFragment - your activity will have just a simple placeholder for a fragment - this should not require much changes, but in the future it would be easier to reformat your code.

Viktor Yakunin
  • 2,927
  • 3
  • 24
  • 39
  • i'm integratin a code into an existing solution. but that what i did, i'm chaging my code now ;) – Fakher Aug 11 '15 at 12:07