startActivity(intent) is doing nothing

半腔热情 提交于 2019-12-11 18:44:47

问题


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.


回答1:


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

  • 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.

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!




回答2:


if you are using custom toolbar to inflate menu then please set toolbar :-

setSupportActionBar(toolbar);

then the menu is inflated properly otherwise the problem is not this.



来源:https://stackoverflow.com/questions/54392434/startactivityintent-is-doing-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!