2nd activity android | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

2nd activity android

https://developer.android.com/training/basics/firstapp/starting-activity.html 1. why do we need to declare value in main activity?( public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";) ? cant we just use (public static String EXTRA_MESSAGE;) ? 2.when do you we use getIntExtra?in the examplr they are using getStringExtra

18th Nov 2017, 12:50 AM
oyl
3 Answers
+ 7
2. Since "Welcome" is a String, you have to use getStringExtra(). Suppose the main activity has passed three extra variables (name, age, gender) to next activity. The key-value pairs are as follows: "name" => "Peter" "age" => 15 // key is String but value is integer here "gender" => "male" Now, you'll have to get the values this way: Bundle extras = getIntent().getExtras(); // getting all extra variables String name = extras.getString("name"); int age = extras.getInt("age"); String gender = extras.getString("gender"); Or, if you want to get the "age" value ONLY: int age = getIntent().getIntExtra("age"); NOTE: Keys are always String but datatype of Values may differ. So getExtra/getSringExtra/getIntExtra takes String argument but returns value of appropriate type.
19th Nov 2017, 4:24 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
1. The String value they've used is little confusing. You can use any String you want and it'll work even if it's not declared as final (using final is good practice though). The sendMessage() method of main activity can be written as: public void sendMessage(View view) { // Declaring Intent for 2nd activity Intent intent = new Intent(this, DisplayMessageActivity.class); // Getting user input from EditText (say it's "Welcome") EditText editText = (EditText) findViewById(R.id.editText); String message = editText.getText().toString(); // Assigning a new key-value pair "msg" => "Welcome" // Here "msg" is key and "Welcome" is value // Both key and value are String here intent.putExtra("msg", message); // I've changed here (easier way) // starting the 2nd activity startActivity(intent); } In 2nd activity: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); // First way: step by step breakdown Intent intent = getIntent(); Bundle extras = intent.getExtras(); // getting all extra variables // getting the value of key "msg", that means "Welcome" String message = extras.getString("msg"); // Second way: Shortcut (compiling the above 3 steps) // String message = getIntent().getExtras().getString("msg"); // Third way: since you have only one extra, you may omit the Bundle part // String message = getIntent().getStringExtra("msg"); TextView textView = findViewById(R.id.textView); textView.setText(message); } NOTE: If you use final variable for key, you can access it in following way: declaration: public static final String EXTRA_MESSAGE = "msg"; // or whatever access: String message =getIntent().getStringExtra(MainActivity.EXTRA_MESSAGE);
19th Nov 2017, 4:36 AM
Shamima Yasmin
Shamima Yasmin - avatar
0
thanks
19th Nov 2017, 7:07 AM
oyl