Search code examples
javaandroidandroid-intentparcel

startActivity(intent) is doing nothing


I'm trying to start an activity with a switch statement like that:

public class MainActivity extends AppCompatActivity {

    public static String key = "1010";
    private Bitmap bitmap = null;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.items, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add:
                //working
                break;
            case R.id.rotate_right:
                //working
                break;
            case R.id.fullscreen: //not working
                Intent intent = new Intent(getBaseContext(), fullScreenActivity.class);
                intent.putExtra(key, bitmap);
                startActivity(intent);
                break;
        }
    }
}

And that's the next activity's code

public class fullScreenActivity extends AppCompatActivity {

    Bitmap bitmap = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Intent starter = getIntent();
        bitmap = starter.getParcelableExtra(MainActivity.key);
    }
}

I also added the second activity to the manifest:

<activity android:name=".fullScreenActivity"
        android:screenOrientation="landscape"></activity>

Here's R.menu.items:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/add"
    android:icon="@drawable/ic_add_black_24dp"
    android:title="@string/add_item"
    app:showAsAction="ifRoom"
    />

<item
    android:id="@+id/rotate_right"
    android:icon="@drawable/ic_rotate_right_black_24dp"
    android:title="@string/rotate_item"
    app:showAsAction="ifRoom"
    />

<item
    android:id="@+id/fullscreen"
    android:icon="@drawable/ic_fullscreen_black_24dp"
    android:title="@string/fullscreen_item"
    app:showAsAction="ifRoom"
    />

</menu>

It does nothing, so what can be the problem?

Note that just the fullscreen item is not working, so I think the problem is in the startActivity code.


Solution

  • I think the problem is this line

    intent.putExtra(key, bitmap);
    

    Android limits size in intent's data. There are 2 cases

    • The app will crash with a RuntimeException, you can easily see this exception in logcat like below screenshot

    enter image description here

    • The app won't crash but showing nothing and back to home screen (your case). Actually there is a warning about this issue, to see the warning you must change logcat type to Warn and logcat filter to No Filters like below screenshot.

    android.os.TransactionTooLargeException

    As you can see the warning like, 812104 in my case, it might be different in your case.

    android.os.TransactionTooLargeException: data parcel size 812104 bytes
    

    Solutions: If you need to pass a bitmap from an activity to another

    • If the bitmap is a URL (file path, http/https links, etc) or a resource id (R.drawable.splash_screen, etc) then just pass the URL or resource id to another activity.
    • If the bitmap comes from a users' action (captured from camera for example) then you can save the bitmap in a local file then pass the file path to another activity.

    Hope this helps!