javaandroidandroid-studioandroid-fragmentsandroid-threading

How to make a new thread, Android Studio?


I have the following fragment class:

public class fragment1 extends Fragment {
    private TextView bunz_count;
    private TextView money_count;
    private Bunz bunz;
    private Handler handler;
    int delay = 1000;
    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        bunz = Bunz.getInstance();
        handler = new Handler();


        view = inflater.inflate(R.layout.fragment1, container, false);


        handler.postDelayed(new Runnable(){
            public void run(){
                update(view);
                handler.postDelayed(this, delay);
            }
        }, delay);







        return view;
    }


    public void update(View view){
        bunz_count = (TextView) view.findViewById(R.id.final_bunz_count);
        money_count = (TextView) view.findViewById(R.id.final_money_count);
        //System.out.println(bunz.getBaker1());


        BigDecimal number = ((BigDecimal.valueOf
                (bunz.getBaker1()).multiply(BigDecimal.valueOf(.1))));
//        ).add((BigDecimal.valueOf(bunz.getBaker2()).multiply(BigDecimal.valueOf(.2)))).
//                add((BigDecimal.valueOf
//                        (bunz.getBaker3()).multiply(BigDecimal.valueOf(.4)))).
//                add((BigDecimal.valueOf
//                        (bunz.getBaker4()).multiply(BigDecimal.valueOf(.8)))).
//                add((BigDecimal.valueOf(bunz.getBaker5()).multiply(BigDecimal.valueOf(1)))).
//                add((BigDecimal.valueOf(bunz.getBaker6()).multiply(BigDecimal.valueOf(2)))).
//                add((BigDecimal.valueOf(bunz.getBaker7()).multiply(BigDecimal.valueOf(4)))).
//                add((BigDecimal.valueOf(bunz.getBaker8()).multiply(BigDecimal.valueOf(5)))).
                //add((BigDecimal.valueOf(bunz.getBaker9()).multiply(BigDecimal.valueOf(10))));
        //System.out.println(number);
        bunz.setBunz(bunz.getBunz().add((number)));
        bunz_count.setText("Bunz: " + bunz.getBunz());
        money_count.setText("Money: " + bunz.getMoney());
        System.out.println("bunz" + bunz.getBunz());

    }
}

which updates the UI display of a players currency. However, since there are lots of different processes running in the background, this thread lags and has problems. How can I run this on a separate thread to avoid this?

Edit: I tried something like this:

        mHandlerThread = new HandlerThread("yeye");
        mHandlerThread.start();
        handler = new Handler(mHandlerThread.getLooper());
        handler.postDelayed(new Runnable(){
            public void run(){
                update(view);
                handler.postDelayed(this, delay);
            }
        }, delay);

but it didn't help!

Thanks!


Solution

  • I have encountered the same problem.

    The solution for my issue was to create 3 runnables and start them in onCreate() Method.

    It looks like that:

    Thread thread = new Thread(runnable);
    thread.start();
    

    In order to create runnable "object" just do the following:

    Runnable runnable = new Runnable(){
        public void run() {   
           //some code here
        }
    }; 
    

    If you want some delayed action, you can work with post delayed in runnable interface (beware, postDelayed() would just repeat the whole runnable. You can avoid that, by adding some conditions)

    Runnable runnable = new Runnable(){
        public void run() {   
           //some code here
           handler.postDelayed(this, 1000);
        }
    }; 
    

    If you want to update GUI, you should invoke following command inside your runnable:

    handler.post(new Runnable() {
         @Override
         public void run () {
         // upate textFields, images etc...
    
         }
    });
    

    P.S. If you have multiple threads and they must be started at different time you can start them from Runnables.

    Some pages that can be helpful:

    new Runnable() but no new thread?

    What's the difference between Thread start() and Runnable run()

    How to run a Runnable thread in Android at defined intervals?

    When to use handler.post() & when to new Thread()