5

I'm facing problem with replacing fragment using FragmentManager. My problem is freezing the UI. I'm trying to find some good practices and/or library to handle my problems.

Part of my code:

Main activity layout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</android.support.v4.widget.DrawerLayout>

Main activity class

public class MyActivity extends Activity 

with method

  public void nextFragment(Fragment fragment, int position) {
    fragment.setArguments(new Bundle());
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager
            .beginTransaction()
            .replace(R.id.content, fragment).commit();

}

Each of my fragment is like

import android.app.Fragment;

public class SomeFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    RelativeLayout rootView = (RelativeLayout) inflater.inflate(
            R.layout.fragment_layout, container, false);
    //many long time data downloading & creating view
    }

private void nextFragment() {

        ((MyActivity) getActivity()).nextFragment(
                new SomeNextFragment(), 0);

    }
}

Now because of downloading data on each fragment start my UI is freezing. Thanks in advance for any help.

Meryl
  • 193
  • 3
  • 10
  • You are using a support.v4 layout with android.app.Fragments and I'm not sure why. Change `extends Activity` to `extends FragmentActivity`, change `getFragmentManager()` to `getSupportFragmentManager()`. Change the `android.app.Fragment` import to `android.support.v4.Fragment`. – EpicPandaForce Jul 21 '14 at 15:25
  • I'm using navigationDrawer and via android developers site example I'm using this example – Meryl Jul 21 '14 at 18:56
  • 2
    But i'm not kidding, you should switch the fragment to support fragment – EpicPandaForce Jul 21 '14 at 21:46
  • I understand, but why should I change to support fragment? – Meryl Jul 21 '14 at 22:46
  • @Meryl, `getFragmentManager()` is API level 11. `getSupportFragmentManager` is supported by the support library. And if you leave it as `Activity` instead of `FragmentActivity`, then you can't use `getSupportFragmentManager`. – Reed Jul 25 '14 at 17:34
  • @Jakar Im targeting only android 4.x + – Meryl Jul 26 '14 at 19:53
  • Okay. Then forget what I said about API levels and the support stuff. It's irrelevant given your case. – Reed Jul 28 '14 at 09:59

1 Answers1

2

As a rule, you should not do the long running/blocking operations on the UI thread. Either use a worker thread or an AsyncTask.

Generally, I would suggest that you create whatever you can in onCreateView then set values after the background operation is complete. For example, create a TextView right away, then when you get the result from the background operation, then set the text in the already existing TextView.

To use a thread:

final Handler handler = new Handler();
new Thread(new Runnable(){
    @Override
    public void run(){
       //long running code
       //this is running on a background thread
       final String someText = //result of long running code
       handler.post(new Runnable(){
           @Override
           public void run(){
               //since the handler was created on the UI thread,
               //   this code will run on the UI thread
               someTextView.setText(someText);
           }
       });
    }
}).start();

You can also use getActivity().runOnUiThread(new Runnable(){...}); instead of using Handler

Community
  • 1
  • 1
Reed
  • 14,703
  • 8
  • 66
  • 110