Transfer data from one screen to another on android eclipse - android

Hi am newbie to java and android.
Assume function demo() from the screenone is going to display some values on Textview which is on the same screen(screenone).
But i need to display that resultant value to the next screen ie.(screen two)
public void demo(){
{
.....
.....
}
So i have included these line to
screenoneActivity.java
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
demo();
ScreentwoActivity.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
txtName.setText(name);
I did these things so far.I don't know how to do transfer data from demo() function to the next screen.
Can anyone give me clues or ideas to achieve this.
Thanks a lot!..

You need to send some value into the putExtra methods parameters to be able to get something out of it.
In your first activity(A):
Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);
In your second activity(B):
Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");

Write the below code in demo() function:
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
In nextScreen.putExtra("",""); provide some key and value like:
nextScreen.putExtra("name","ABC");
Now in SecondActivity, write:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
Bundle bundle = i.getExtras();
txtName.setText(bundle.getString("name"));

In ScreenoneActivity
Intent act2=new Intent(this,Activity2.class);
act2.putExtra("A",a);
startActivity(act2);
In ScreentwoActivity class
Intent i = getIntent();
Bundle extras = getIntent().getExtras();
int a = extras.getInt("A");
txtName.setText(a);

in onCreate :
Bundle extras = getIntent().getExtras();
String value;
if (extras != null)
{
value= extras.getString("key");
}
https://stackoverflow.com/questions/10752501/how-can-we-go-to-next-page-in-android/10752516#10752516
google it is very basic .....
android using intent....
Vogella Ariticle
in activity 1-
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
startActivity(i);
In activity 2 - in onCreate finction
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}

Related

android data passing to another class by a button click

Im trying to pass data from my TestSearch class to GoogleSearch class. I assigned the values of the tEdit test to a string variable and I want to pass that to GoogleSearch class. But my app get crash when I run it.
Button disp=(Button)findViewById(R.id.btn_Search);
disp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText inputTxt = (EditText) findViewById(R.id.editText1);
String str = inputTxt.getText().toString().toLowerCase().trim();
ArrayList<HashMap<String, String>> userList = controller.searchBook(str);
if (userList.size() != 0) {
//Do something
}
else
{
Intent intent = new Intent("com.example.captchalib.GoogleSearch");
intent.putExtra("message", str);
startActivity(intent);
}
}
});
In my GoogleSearch Class I used Below Code to catch the Intent
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_menu);
Intent intent = getIntent();
String message = intent.getStringExtra("message");
((TextView)findViewById(R.id.Receive)).setText(message);
}
why this is happening and how to solve it ?
Logcat
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.captchalib.GoogleSearch (has extras) }
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
Change this line:
Intent intent = new Intent("com.example.captchalib.GoogleSearch");
To:
Intent intent = new Intent(TestSearch.this, GoogleSearch.class);

Show Selected value from spinner in one activity to the textview in another activity

I know there are few questions similar to mine and I have tried all the suggested solutions mentioned in the existing questions however it's still not working. Pretty sure I'm doing something wrong logically but unable to figure out where. Please point me to right direction.
Spinner activity code method
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
// TODO Auto-generated method stub
Intent main = new Intent (this, MainActivity.class);
Bundle myBundle = new Bundle();
String chosenAppType = appliancestypespinner.getSelectedItem().toString();
myBundle.putString("appliance type spinner",chosenAppType);
main.putExtra("chose appliance type", myBundle);
startActivity(main);
}
TextView Activity (getting the spinner value as string in textview (tvApplianceType = textview )
Bundle myBundle = this.getIntent().getExtras();
if(myBundle == null)
{
return;
}
String str_recieved_appType = myBundle.getString("appliance type spinner");
if (str_recieved_appType != null)
{
tvApplianceType.setText(str_recieved_appType);
}
}
Just to add that I'm also passing the value of Edittext present from the Spinner activity to the same textview that i'm passing the spinner value to that EditText to TextView is working fine. Is it not working because i'm using the same textview for both operations however either operation need to work at one time :/. So either edittext to textView OR spinner to textview.
Edittext Code
String str_appliance_type = et_input_Appliance_Type.getText().toString().trim();
if (!str_appliance_type.equals(""))
{
data.add(str_appliance_type );
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data );
appliancestypespinner.setAdapter(aa);
//passing edittext value to MainActivity class
Bundle carrier = new Bundle();
Intent i = new Intent (this, MainActivity.class);
i.putExtra("appliance type",str_appliance_type);
startActivity(i);
}
Passing Value fro Editext to textview
//getting the edittext value from inputAppliance class
Bundle carrier = getIntent().getExtras();
if(carrier == null)
{
return;
}
String str_recieved = carrier.getString("appliance type");
if (str_recieved != null)
{
tvApplianceType.setText(str_recieved);
}
String str = appliancestypespinner.getSelectedItem().toString(); // I assume this line is proper.
Intent i = new Intent (this, MainActivity.class);
Bundle b = new Bundle();
b.putString("your_key", str);
i.putExtras(b);
startActivity(i);
In MainActivity
Bundle b = getIntent().getExtras();
String s = b.getString("your_key");
your_textView.setText(s);
Intent main = new Intent (this, MainActivity.class);
main.putExtra("SELECTED_DATA", appliancestypespinner.getSelectedItem().toString());
startActivity(main);
In Another Activity:
String data=this.getIntent.getStringExtra("SELECTED_DATA");
Try using Intent:
Spinner Activity:
Intent calculate = new Intent(getApplicationContext(),Second.class);
calculate.putExtra("carry_name", entername.getText().toString());
On Second Activity:
Intent intent = getIntent();
String name = intent.getStringExtra("carry_name");
congrates.setText("Congratulation " + name);
Change this
main.putExtra("chose appliance type", myBundle);
startActivity(main);
to
main.putExtras(myBundle);
startActivity(main);

How to send datafrom one activity to another in Android? [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 8 years ago.
My app has 2 activites.
The first activity is just a simple form where a user enters course information(class title, professor..etc.) the first activity passes the data which is supposed to be stored in a list.
In the second activity. The problem is that only the first course gets stored in the list, after the first time nothing new gets added to the second activity.
How can i do this ?
In your first activity :
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("login", jObj.getString(KEY_LOGIN));
intent.putExtra("mdp", jObj.getString(KEY_MDP));
intent.putExtra("prenom", jObj.getString(KEY_PRENOM));
intent.putExtra("nom", jObj.getString(KEY_NOM));
intent.putExtra("mail", jObj.getString(KEY_MAIL));
intent.putExtra("tel", jObj.getString(KEY_TEL));
startActivity(intent);
In your second activity :
Intent intent = getIntent();
if (intent != null) {
login = intent.getStringExtra("login");
mdp = intent.getStringExtra("mdp");
items.add(intent.getStringExtra("login"));
items.add(intent.getStringExtra("prenom"));
items.add(intent.getStringExtra("nom"));
items.add(intent.getStringExtra("mail"));
items.add(intent.getStringExtra("tel"));
}
Generally you can pass data from one Activity to another with an Intent like this:
In your first Activity:
// Create your Intent
Intent intent = new Intent(context, TargetActivity.class);
// Now you can add extras to the intent, you identify extras with a String key
intent.putExtra("text", someString);
intent.putExtra("amount", someInteger);
// Then you start your Activity with this Intent
startActivity(intent);
In your second Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Only get data from Intent when the Activity is new
if(savedInstanceState == null) {
Intent intent = getIntent();
// Now you read the values from the Intent
String someString = intent.getStringExtra("text");
int someInteger = intent.getIntExtra("amount", 0);
}
}
hey its simple look at the code
for sending
Intent i = new Intent(getApplicationContext(), Confirmation.class)
i.putExtra("name",etName.getText().toString()));
i.putExtra("pass",etPass.getText().toString());
startActivity(i);
for recieving in next activity
Bundle extras = getIntent().getExtras();
String strEmployeeID="";
if (extras != null)
{
String value = extras.getString("name");
String value1 = extras.getString("pass");
//
Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
strEmployeeID = value;
strEmployeePass = value1;
}
just simply do it like that:
passing data from 1st activity to 2nd activity:
Intent intent = new Intent(yourafirstctivity.this,Yoursecondactivity.class);
intent.putExtra("yourkey", yourvalue);
intent.putExtra("yourkey", yourvalue);
intent.putExtra("yourkey", yourvalue);
startActivity(intent);
and get the data from 1st activity to 2nd activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.yourlayout);
String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");
}
if you want to send data between two Activities .You must call following one,
startActivityForResult(getcontext(), SecondActivity.class);
On calling this,Activity starts the second.In turn, second response to this intent and reply back with necessary data.
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (mParent == null) {
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
mStartedActivity = true;
}
} else {
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}
Following one you must call this on the First Activity (from where the intent called).It helps to recieves the reply data from second Activity.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!isFeatureInstalled(getContext()))
return;
if(requestCode == GET_REMINDER_DETAILS && resultCode == Activity.RESULT_OK)
{
mFilled = true;
fillDataFromIntent(data);
checkALertRequired.setChecked(true);
}
}

Intent — transferring a button tag?

I have a borderless button on in one of my layouts. When it is clicked, I want the intent to transfer both the button's tag and the text it contains. I am having trouble doing this.
This is what I have for when the button is clicked:
public void openGoalWeek (View view) {
Intent intent = new Intent(this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
}
This is what I have in the ViewGoal class:
public class ViewGoal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_goal);
// make sure running on honeycomb or higher for actionbar API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// get the message from the intent
Intent intent = getIntent();
String message = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE).toString();
String tag = intent.getBundleExtra(MainActivity.EXTRA_TAG).toString();
String text = "null";
if (tag == "year_tag") {
DatabaseHandler db = new DatabaseHandler(this);
Goal goal = db.getYearGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "month_tag") {
DatabaseHandler db = new DatabaseHandler(this);
MonthGoal goal = db.getMonthGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "week_tag") {
DatabaseHandler db = new DatabaseHandler(this);
WeekGoal goal = db.getWeekGoal(message);
text = goal._title + "\n" + goal._description;
}
// create the text view
TextView textView = new TextView(this);
textView.setTextSize(10);
textView.setText(text);
// display the content
setContentView(textView);
}
}
It throws an error at me and I am not sure why. Any help is appreciated!
why don't you just do this, just put your EXTRA instead of using bundle:
...
Intent intent = new Intent(this, ViewGoal.class);
intent.putExtra( EXTRA_MESSAGE, button.getText().toString());
intent.putExtra( EXTRA_TAG, button.getTag().toString());
....
Later on in the other activity:
....
// get the message from the intent
Intent intent = getIntent();
String id = intent.getStringExtra(EXTRA_MESSAGE);
String name = intent.getStringExtra(EXTRA_TAG);
....
In onClick button, instead of this used classname.this
Some of my errors are removed by this. Try if it helps you.
public void openGoalWeek (View view) {
Intent intent = new Intent(className.this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);

Bundle is null after setting it in Intent

I know there are questions like:
android-intent-bundle-always-null and intent-bundle-returns-null-every-time but there is no correct answer.
In my Activity 1:
public void goToMapView(Info info) {
Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra("asdf", true);
info.write(intent);
startActivity(intent);
}
In Info:
public void write(Intent intent) {
Bundle b = new Bundle();
b.putInt(AppConstants.ID_KEY, id);
... //many other attributes
intent.putExtra(AppConstants.BUNDLE_NAME, b);
}
public static Info read(Bundle bundle) {
Info info = new Info();
info.setId(bundle.getInt(AppConstants.ID_KEY));
... //many other attributes
return info;
}
In MapViewActivity (Activity 2):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
Bundle extras = getIntent().getBundleExtra(AppConstants.BUNDLE_NAME);
info = Info.read(extras);
...
}
Problem is that extras bundle is always null. I have debugged it and Intent (intent = getIntent()) has all fields set to null except one indicating what class this is (MapViewActivity).
I've also tried putting the bundle via intent.putExtras(b) with the same effect.
The intent.putExtra("asdf", true) is for debugging reasons only - I cannot get this data too (because getIntent() has almost all fields set to null).
EDIT
The answers below are correct and working. This was my fault. I didn't correctly passed my bundle to new intent.
I am not sure what "Info" is for, but I suggest making the most basic passing of data from one activity to another first before involving other data objects.
Activity1
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("asdf", true);
info.write(intent);
startActivity(intent);
Activity2
Bundle bundle = getIntent.getExtras();
if (bundle!=null) {
if(bundle.containsKey("asdf") {
boolean asdf = bundle.getBooleanExtra("asdf");
Log.i("Activity2 Log", "asdf:"+String.valueOf(asdf));
}
} else {
Log.i("Activity2 Log", "asdf is null");
}
Activity 1
Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
Bundle b = new Bundle();
b.putBoolean("asdf", true);
b.putInt(AppConstants.ID_KEY, id);
intent.putExtras(b);
startActivity(intent);
Activity 2
Bundle extras = getIntent().getExtras();
boolean bool = extras.getBoolean("asdf");
int m_int = extras.getInt(AppConstants.ID_KEY,-1);

Categories

Resources