2

I have the following code:

   ((ImageButton)findViewById(R.id.fish)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(StartupActivity.this, GameActivity.class);
            intent.putExtra(NAME_EXTRA, ((EditText)findViewById(R.id.name)).getText().toString().trim());
            intent.putExtra(TYPE_EXTRA, FishTypes.FISH.toString());
            intent.putExtra(WORLD_TYPE_EXTRA, worldType);
            intent.putExtra(LOGO_EXTRA, BitmapFactory.decodeResource(getResources(), R.drawable.logo));
            startActivity(intent);
        }
    });

If I start activity and click on ImageButton it send me to the same Activity I was before (StartupActivity). However if I comment out the last putExtra like this:

//intent.putExtra(LOGO_EXTRA, BitmapFactory.decodeResource(getResources(), R.drawable.logo));

Then it works fine. It sends me to the GameActivity as I want. What could be the problem?

EDIT

My GameActivity looks like this:

public class GameActivity extends Activity {

public static GamePanel gamePanel;
public static String name;
public static FishTypes type;
public static String worldType;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    requestWindowFeature(getWindow().FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);


    name = getIntent().getStringExtra(StartupActivity.NAME_EXTRA);
    type = FishTypes.parse(getIntent().getStringExtra(StartupActivity.TYPE_EXTRA));
    worldType = getIntent().getStringExtra(StartupActivity.WORLD_TYPE_EXTRA);

    gamePanel = new GamePanel(this);
    //gamePanel.bitmaps.put("logo", (Bitmap)getIntent().getParcelableExtra(StartupActivity.LOGO_EXTRA));
    setContentView(gamePanel);

    if(!StartupActivity.isNetworkAvailable()) {
        Toast.makeText(StartupActivity.getInstance(), "You have no internet connection...", Toast.LENGTH_LONG).show();
        finish();
    }
}

@Override
protected void onDestroy() {
    if(gamePanel.client != null)
        gamePanel.client.disconnect();
    StartupActivity.getInstance().reopen();
    super.onDestroy();
}
}

What I want to achieve is preloading this bitmap in StartupActivity and then just send it to the GameActivity to the GamePanel and then draw it on the canvas as a loading image. I can't load this image in GameActivity because it will be late to show it. Do you understand?

duri
  • 71
  • 2
  • 8

1 Answers1

0

So first of all, there is an Intent payload limit, as far as I know there is a limit of 1MB, but in some cases can be 500kb. If this limit is overreached the app will crash, in your case crashed and restarted.

Second of all, Bundle and Intent are used to send small amount of data to Activity and Fragments, usually some configs/params so that the activity/fragment will know how to build itself.

Third, is a really bad practice to pass around instances of bitmaps, you need just a second of distraction to create a huge leak in your app, that will cost you a lot of time to find and fix it.

Now the solution for you is really simple. You can pass the id of the bitmap you want to use in the next activity.

 Intent intent = new Intent(StartupActivity.this, GameActivity.class);
        intent.putExtra(NAME_EXTRA, ((EditText)findViewById(R.id.name)).getText().toString().trim());
        intent.putExtra(TYPE_EXTRA, FishTypes.FISH.toString());
        intent.putExtra(WORLD_TYPE_EXTRA, worldType);
        intent.putExtra(LOGO_EXTRA,  R.drawable.logo); // R.drawable.logo is actually an int.
        startActivity(intent);

In your GameActivity

    @Override
public void onCreate(Bundle savedInstanceState) {
    setContentView(...)
    int myAwesomeDrawable = getIntent().getExtra(LOGO_EXTRA, 0); // 0 is default value in case nothing is added to the key
    if(myAwesomeDrawable != 0){ // safety check
      imageView.setImageResource(myAwesomeDrawable);
      // or do whatever you like with it.
    } 
danypata
  • 9,895
  • 1
  • 31
  • 44
  • Nearly the same I wrote before. Until I realized, R.drawable.logo can be accessed in the target activity anyway. This is why you don't need to pass the logo at all as an extra. – AlbAtNf Jul 29 '16 at 14:47
  • But I need it to preload in the StartupActivity... this answer is wrong. I don't want to load it in GameActivity because it will take time to load and it should be the loading image. Understand? – duri Jul 29 '16 at 16:41
  • @duri Loading an image from assets doesn't take time is almost instant. – danypata Jul 29 '16 at 21:22