-1

I am currently working with fragments,i am try to load the data from Database into fragments class,once i run the application my previous data saved my class, cannot Reload data whenever i updated database, I am also refer this refresh fragment at reload But it not work for me,

My code was,

   `Fragment fragment;
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame,fragment);
    ft.detach(fragment);
    ft.attach(fragment);
    ft.commit();`

And my packages import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction;

please help me friends!!

Community
  • 1
  • 1
Vijay
  • 227
  • 3
  • 18
  • 2
    your fragment is null..use atleast on fragment – Ranjit Mar 30 '17 at 07:33
  • i am also in my project i will give != null case R.id.nav_dashboard: fragment = new FmDashboard(); break; case R.id.nav_store: fragment = new FmStore(); break; But its holds previous data!! – Vijay Mar 30 '17 at 07:42
  • The code your referred has one fragment tag like `frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");` .. you can refresh the current fragment by getting the current fragment through its TAG and then by detaching and re attaching it. – Ranjit Mar 30 '17 at 07:46
  • Any example to create tag – Vijay Mar 30 '17 at 07:49
  • i get this friend,,java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference – Vijay Mar 30 '17 at 08:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139473/discussion-between-vijay-kumar-and-ranjit-pati). – Vijay Mar 30 '17 at 09:04

1 Answers1

0

Try my below code once and please have a look here

SomeFragment fragment = null;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,fragment);

//no need to detach
//ft.detach(fragment);
//no need to attach
//ft.attach(fragment);

//just commit 
ft.commit();`

UPDATE

Lets initiate the fragment and give it some recognition through a TAG like

SomeFragment fragment = new SomeFragment();
getFragmentManager()
        .beginTransaction()
        .add(android.R.id.content, fragment, "my_fragment_tag")//now you can recognize your fragment through "my_fragment_tag" 
        .commit(); 

And now you can refresh your fragment when you need by your detach/attach way

// Reload current fragment
Fragment frg=getSupportFragmentManager().findFragmentByTag("my_fragment_tag");
final FragmentTransaction ft =    getSupportFragmentManager().beginTransaction();
if(frg != null){ // if fragment is removed from memory it will give null pointer exception
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
Keyur Thumar
  • 608
  • 7
  • 19
Ranjit
  • 5,130
  • 3
  • 30
  • 66