2

I made very simple app. Basically I have activity A that starts activity B if button is pressed. So here's the code

openMapFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MapActivity.class);
                intent.putExtra("routeId", id);
                startActivity(intent);
            }
        });

But here's the problem. activity B is very heavy (it basically open map (mapsforge) and do other heavy calculations). So when pressing buton my UI freezes about 4 seconds and then I get activity B opened. I would like to show some animation like circle progress bar or something like that. So is it possible to start activity not in the UI thread?

Here's what I tried so far

 new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (progressStatus < 100) {
                            progressStatus += 25;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    //animation code
                                }
                            });

                            try {
                                Thread.sleep(1600);
                                if (progressStatus == 100){
                                    Intent intent = new Intent(getActivity(), MapActivity.class);
                                    intent.putExtra("routeId", id);
                                    startActivity(intent);
                                }

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();

However this is not exactly what I need. Any solutions or suggestions?

David
  • 3,055
  • 4
  • 30
  • 73
  • If the heavy code is inside MapActivity this won't work. You should try to refactor MapActivity to move code into e.g. threads or asynctasks. Add the code for MapActivity if you need help – mlidal Feb 01 '17 at 08:30

2 Answers2

0

1) So is it possible to start activity not in the UI thread

Activity should be processed in Main UI Thread only. You will not be able to start an activity in other threads. Even after processing data in background thread, for invoking or pushing an activity methods like runOnUiThread is used

If you are performing long operations in main thread, Android will throw ANR

2) However this is not exactly what I need. Any solutions or suggestions?

Now an alternative approach is to do heavy operations of your Activity in background using AsyncTask or IntentService and show progress bar until those operations are completed.

Once this operation is completed, publish the data to your MapActivity.

1) Sample Intent Service implementation

2) Sample AsyncTask example

Sample of AsyncTask on onCreate will be as follows

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new BackAsync().execute("URL");
}
private String getURLDetailsTitle(String url) {
  //Retrieve data from external URL
}
//AsyncTask to fetch data from URL called inside onCreate
private class BackAsyncextends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... urls) {
        String str = getURLDetailsTitle(urls[0]);
        while(true) {
           publishProgress(str);

        }
        return str;
    }

    @Override
    protected void onProgressUpdate(String... progress) {
        //update progress
    }

    protected void onPostExecute(Long result) {
         //update UI changes
     }
Sreehari
  • 5,621
  • 2
  • 25
  • 59
0

Use AsyncTask and start progress bar in the doInBackground method and close it in the onPostExecute method.

Android
  • 54
  • 5