Android additional data accounting lost

I am trying to get some data transferred inside my application using additional features.

I attach the data to the intent as follows:

Intent i = new Intent(ActivityFrom.this, ActivityTo.class);
i.putExtra(CONST_KEY, true);
startActivity(i);

FROM

public static final String CONST_KEY = "MyBooleanValue";

I am trying to get data in a running ActivityTo onCreate method as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extra = getIntent().getExtras();
    if ( extra != null ) {
        boolean b = extra.getBoolean(ActivityFrom.CONST_KEY);
    }
}

However, I never come across an if-block, because Bundlealways null.

Why are my extras lost? What do I need to change to get the extra features that I set for myself?

Edit

  • Fixed typo in code
  • Added full method declaration onCreate
  • Here is the declaration ActivityToin the manifest:

    <activity
        android:name=".ActivityTo"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">
    </activity>
    
+4
source share
3 answers

android:launchMode="singleTask" . , , ActivityTo, . , , onNewIntent(Intent). :

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

, getIntent().

+6

,

if(getIntent().getBooleanExtra(ActivityFrom.CONST_KEY,false)!=null){
     Boolean b=getIntent().getBooleanExtra(ActivityFrom.CONST_KEY,false);
}
-1

You can try this

in Main.java

Intent intent = new Intent(this, YourActivity.class);
String message = constkey.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);

higher oncreate method

public final static String EXTRA_MESSAGE = "com.example.project.MESSAGE";

in YourActivity.java

Intent intent = getIntent();
        String message = intent.getStringExtra(Main.EXTRA_MESSAGE);

how wise.

-2
source

Source: https://habr.com/ru/post/1617945/


All Articles