The application must download an image from the server and set it as an Activity. That's just the image does not have time to load and the background remains "by default"
My background is ImageView and I initialize it in the OnCreate method.

 fon = new ImageView(this); 

and after that I try to install an image into it:

 Picasso.with(getApplicationContext()).load(URL_BACKGROUND).into(fon); 

But Picasso loads it after the onCreate method onCreate worked and fon does not change.

UDP:

There is also such an option of loading the image and installing it as a background:

 mTarget = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { contentNav = (RelativeLayout) findViewById(R.id.content_navigation_drawer); contentNav.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap)); } @Override public void onBitmapFailed(final Drawable errorDrawable) { } @Override public void onPrepareLoad(final Drawable placeHolderDrawable) { } }; Picasso.with(getApplicationContext()).load(URL_BACKGROUND).into(mTarget); 

Only here the background is RelativeLayout and at the same time, the image is loaded very late and will not change at runtime but only after mTarget = new Target() called again

  • You create a ImageView programmatically, I do not know whether it will roll, but it seems, you need to either create in xml or programmatically, but something else is needed. Who is more experienced - correct me - Flippy
  • Yes - if ImageView is not added to the markup, then nothing will be displayed. Cht instantly download, at least for years - YuriySPb

1 answer 1

1) You can create a class public class App extends Application and register it in the manifest

  <application android:allowBackup="true" android:icon="@mipmap/app_ico" android:label="@string/app_name" android:theme="@style/AppTheme" **android:name=".App"** > 

2) Inside this class, which will be loaded BEFORE you start downloading any Activation, inside onCreate you can download a picture and, if you get a result (successful or not), run your activity

 @Override public void onCreate() { new DownloadImage().execute(imageUri); } private class DownloadImage extends AsyncTask<String, Void, Bitmap> { protected Bitmap doInBackground(String... urls) { String urlDisplay = urls[0]; Bitmap mImage = null; try { InputStream in = new java.net.URL(urlDisplay).openStream(); mImage = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("MyLog", "Error when downloading image: " + e.getMessage()); e.printStackTrace(); } return mImage; } protected void onPostExecute(Bitmap result) { //тут сохраняем полученный рисунок (если есть) и стартуем активность } } 
  1. It is important to remember that downloading cannot be done in the main thread. Also, with this solution there will be a slight delay in the display of Activiti (while the download is in progress).

    1. Also, you can make an intermediate activity that will load the image.

    2. Substitute the saved image in your ImageView through Picasso.

    3. Remember that you need to remove the downloaded image when you no longer need it.