So, here is the thing- I have a mainactivity class and a Splash class. The mainactivity contains two TextViews and one button. Now, i have my splash class which has two editTexts and one Ok Button. My goal is that when i run the android program, the main activity loads up. Then i click on the button and the Splash activity loads up. In the splash activity, i enter two numbers in the EditText boxes and the numbers will be captured in the Intent Object and returned in the appropriate TextView lables in the Main Activity. Now, when i try to capture one value and return that value in the original activity(Main Activity), my code works fine but when i try with two values and use the same concept, the second number that i enter in the Splash activity overwrites the first activity and i see the second number repeated twice. How do i fix this? How can i capture more than one values in an intent and return them in their appropriate labels in the Main Activity class. Here is the code:-
MainActivity.java
package com.example.user.androidpractise4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b;
int code = 1;
String a,c;
int num;
int num1;
TextView t,v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.Btn1);
t = (TextView)findViewById(R.id.textView2);
v = (TextView)findViewById(R.id.TextV);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent("com.example.user.androidpractise4.SPLASH"), code);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == code) {
if (resultCode == RESULT_OK) {
// Toast.makeText(this, data.getData().toString(),
//Toast.LENGTH_SHORT).show();
a = data.getData().toString();
num = Integer.parseInt(a);
t.setText("" + num);
//This is the part where i tried to improvise but it doesnt work
c = dat.getData().toString();
num1 = Integer.parseInt(c);
t.setText("" + num1);
int total = num1 + num;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Splash.java:-
package com.example.user.androidpractise4;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by user on 11-08-2015.
*/
public class Splash extends Activity {
Button b1;
EditText display,disp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
b1 = (Button)findViewById(R.id.button);
display = (EditText)findViewById(R.id.editText);
disp =(EditText)findViewById(R.id.editText2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent data = new Intent();
data.setData(Uri.parse(display.getText().toString()));
setResult(RESULT_OK, data);
//I have added the below part to see if i can capture more than one value from //Splash activity using the Intent and return it to the MainActivity.
Intent dat = new Intent();
dat.setData(Uri.parse(disp.getText().toString()));
setResult(RESULT_OK, dat);
finish();
}
});
}
}