Search code examples
androidandroid-intentandroid-activity

Android - How to fully load an activity in the background


The goal is to run and load a second activity in the background when the user is still in the first activity. The second activity is pretty heavy and it takes time to load; so I need to show the second activity after it's loaded and ready.

public void onButtonClick(View view){
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("user_input", userInput.getText().toString());

    startActivity(intent);
}

Can I modify the above code in order to load the second activity in the background?


Solution

  • if you have heavy task in you second activity than move your heavy task into the background service or thread.

    you can use AsyncTask for background work

    AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

    Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive.

    example of AsyncTask

    link 1

    link 2

    link 3