简体   繁体   中英

Dedicated Android Activity to initialize data and libraries for the application

My app makes use of various heavy libraries and data files, that need to be loaded / synchronized with local storage on start-up. As this operation takes some time, I was thinking of creating a dedicated activity for this purpose. This activity would be the entry point of the application and would do the following:

  • Display a background image and progress bar
  • Process necessary data (and updating the progress bar accordingly)
  • On completion, launch a new activity

Here is a couple of questions:

  • My initialization activity needs to pass a reference to the data to its child activity. Ordinarily I would do this with setSerializable , but here I am using 3rd-party non-serializable classes.

So I was thinking of simply making the data static. Is this a good solution? Any chance the static reference might break when switching activities or during the application life-cycle?

public class LibraryInitializer {

   private static Some3rdPartyClass myLib;

   public static void initialize(){ // Called by initialization activity
      myLib = Some3rdPartyClass.create();
   }

   public static Some3rdPartyClass getMyLib(){ // Called by child activity
      return myLib;
   }
}
  • I need the initialization activity to be called only once. How to prevent that it is shown again when a user clicks on the back button?

More generally, is this approach okay, or would you suggest a better one? (I also considered using one single activity, but adding/removing the progress-bar and background dynamically)

Long running code really ought to be done using Android services . I would recommend doing your complicated logic in a service that your various "Activity" classes (which correspond to the views of your application) simply consume. Services can outlive the UI of the application and can also be started / initialized in response to various other events (like system boot), even when the user is not interacting with the application whereas an Activity is very tightly intertwined with the presentation.

In terms of loading / syncing data, I would strongly recommend against putting this logic in your activity code... instead, use a SyncAdapter or one of the other scheduling mechanisms for syncing. This will ensure that syncing activity is batched with other uses of the networking chip, which minimizes overall battery usage, and also allows you to have synced before the user is actively using your application, so that you aren't keeping the user waiting when they open your app.

When you start a new Activity from the loading Activity call the finish() method to close that activity and it will be deleted from the applications activity stack. like below:

startActivity(intent);
finish();

For the question for passing data through activities, extend the Android Application class, where you can define the global state and variables of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM