1

I have code that direct users to the profile page from the bottom bar.

private void startNewIntent(Class className, String uid){
    Intent intent = new Intent(act, className);
    intent.putExtra("uid", uid);
    act.startActivity(intent);
    act.finish();
}

className = DisplayProfile.class;
if(FirebaseAuth.getInstance().getCurrentUser() != null){
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    startNewIntent(DisplayProfile.class, uid);
} else {
    startNewIntent(EmailPasswordActivity.class);
}

in ProfileActivity.java

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    String uid = getIntent().getStringExtra("uid");
    if(uid != null){
        ...
    }
}

I also tried with Bundle = getIntent().getExtra() with the same results. I have seen similar questions. This seem to be the case: getIntent() Extras always NULL

I tried

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

but getIntent().getExtra != null is still false.

Thank you for your help and advice.

Edit: added context for startNewIntent()

Community
  • 1
  • 1
dendrobium
  • 81
  • 2
  • 9

2 Answers2

2

Rather than checking in onCreate() check in onNewIntent():

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        String uid = getIntent().getStringExtra("uid");
        if(uid != null){
            ...
        }
    }
    
    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        String uid = getIntent().getStringExtra("uid");
        if(uid != null){
            ...
        }
    }

If it's coming in onNewIntent() this means you are using launchMode for your activity. So this is the behaviour of launch mode.

Ready Android
  • 3,529
  • 2
  • 26
  • 40
  • Can you explain launchMode to me? Thank you – dendrobium Sep 13 '16 at 14:45
  • In the android activity, there are four types of activity launch modes. standard, singleTop, singleTask, singleInstance. These four are used to manage activity flow with stack management. For more details follow android:launchMode at: [link](https://developer.android.com/guide/topics/manifest/activity-element.html) – Ready Android Sep 14 '16 at 08:38
  • There was the same problem when returning to the MainActivity and invoke getExtra() from onResume(). This variant solved the problem. – alexrnov Apr 14 '20 at 04:37
  • I had to use like this after updating to compileSdkVersion 30. Always worked before directly at onCreate... i don't know what happened here. – Thalis Vilela Dec 09 '21 at 14:35
  • @ThalisVilela this is not due to compileSdkVersion 30 or any other just check your AndroidManifest.xml for launch mode added to that particular activity tag or not? Might be you are launching activity with any intent flag which is generating this cause – Ready Android Dec 10 '21 at 06:41
  • @ReadyAndroid, i didn't change the manifest in a long time, perhaps the startActivity without flags are defaulting to a different mode. In this particular case, i'm using startActivityForResult by the way. – Thalis Vilela Dec 10 '21 at 20:20
0

Avoid ".class" if already present in your class name.

public void startNewIntent(Class className, String uid){
Intent intent = new Intent(act.this, className+".class");
intent.putExtra("uid", uid);
startActivity(intent);
}

Next Phase

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String uid = getIntent().getExtras().getString("uid");
if(uid != null){
    ...
 }
}