Passing Intent can't get extra();

后端 未结 4 1371
独厮守ぢ
独厮守ぢ 2021-01-16 17:08

Hi i can\'t get logged in already in a web page at mine main activity i passed inten to another activity but i can\'t get an extract. How can i solve it

this is min

相关标签:
4条回答
  • 2021-01-16 17:25

    Try this way,hope this will help you to solve your problem.

    There is two way pass/get data one activity to another activity.

    1.add data to intent.

    how to put :

    Intent goToNextActivity = new Intent();
    goToNextActivity.putExtra("username", user_name);
    goToNextActivity.putExtra("password", pass_word);
    

    how to get :

    String username = getIntent().getStringExtra("username");
    String password = getIntent().getStringExtra("password");
    

    2.Add data to bundle and add bundle to intent.

    how to put :

    Intent goToNextActivity = new Intent();
    Bundle bundle = new Bundle();
    bundle.putString("username", user_name);
    bundle.putString("password", pass_word);
    goToNextActivity.putExtras(bundle);
    

    how to get :

    String username = getIntent().getExtras().getString("username");
    String password = getIntent().getExtras().getString("password");
    
    0 讨论(0)
  • 2021-01-16 17:31

    Here

    Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);

    you are constructing a brand new Intent which is not what you want. You want to use the Intent that started the Activity.

    For this reason, you want to use

    Intent i = getIntent();
    

    because we can see from the Activity Docs that getIntent()

    Return the intent that started this activity.

    then get the extras if they are not null

    if (i.getExtras() != null) {
        String userName = i.getStringExtra("username");
        String password = i.getStringExtra("password");  // you have a typo in your passing Intent
    }
    

    You need your Activity Context for this so it is better/easier to do it in onCreate() instead of your inner-class unless there is a reason you can't do it there.

    0 讨论(0)
  • 2021-01-16 17:44

    Use just below code for getting data from intent:-

    Intent intent = getIntent();
    String username= intent.getStringExtra("username");
    

    Write above code in shouldOverrideUrlLoading method.

    0 讨论(0)
  • 2021-01-16 17:46
    Intent extras = getIntent().getExtras();
    String newString;
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("username");
    }
    

    and for two values both username and password you are giving same key values...please change and try it.

    0 讨论(0)
提交回复
热议问题