I want to show edit text from ExplicitlyLoadedActivity in ActivityLoaderActivity.
ActivityLoaderActivity
package course.labs.intentslab;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ActivityLoaderActivity extends Activity {
static private final int GET_TEXT_REQUEST_CODE = 1;
static private final String URL = "http://www.google.com";
static private final String TAG = "Lab-Intents";
// For use with app chooser
static private final String CHOOSER_TEXT = "Load " + URL + " with:";
// TextView that displays user-entered text from ExplicitlyLoadedActivity runs
private TextView mUserTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loader_activity);
// Get reference to the textView
mUserTextView = (TextView) findViewById(R.id.textView1);
// Declare and setup Explicit Activation button
Button explicitActivationButton = (Button) findViewById(R.id.explicit_activation_button);
explicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startExplicitActivation() when pressed
#Override
public void onClick(View v) {
startExplicitActivation();
}
});
// Declare and setup Implicit Activation button
Button implicitActivationButton = (Button) findViewById(R.id.implicit_activation_button);
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
#Override
public void onClick(View v) {
startImplicitActivation();
}
});
}
// Start the ExplicitlyLoadedActivity
private void startExplicitActivation() {
Log.i(TAG,"Entered startExplicitActivation()");
// TODO - Create a new intent to launch the ExplicitlyLoadedActivity class
Intent explicitIntent = new Intent (ActivityLoaderActivity.this,ExplicitlyLoadedActivity.class);
// TODO - Start an Activity using that intent and the request code defined above
startActivity (explicitIntent);
}
// Start a Browser Activity to view a web page or its URL
private void startImplicitActivation() {
Log.i(TAG, "Entered startImplicitActivation()");
// TODO - Create a base intent for viewing a URL
// (HINT: second parameter uses Uri.parse())
Intent baseIntent = new Intent (Intent.ACTION_VIEW,Uri.parse(URL));
// TODO - Create a chooser intent, for choosing which Activity
// will carry out the baseIntent
// (HINT: Use the Intent class' createChooser() method)
Intent chooserIntent = Intent.createChooser(baseIntent,CHOOSER_TEXT);
Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
// TODO - Start the chooser Activity, using the chooser intent
startActivity (chooserIntent);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
// TODO - Process the result only if this method received both a
// RESULT_OK result code and a recognized request code
// If so, update the Textview showing the user-entered text
// Check which request we're responding to
if (requestCode == GET_TEXT_REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
String input = data.getStringExtra("BAG");
mUserTextView.setText(input);
}
}
}
}
'
ExplicityLoadedActivity
package course.labs.intentslab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ExplicitlyLoadedActivity extends Activity {
static private final String TAG = "Lab-Intents";
private EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.explicitly_loaded_activity);
// Get a reference to the EditText field
mEditText = (EditText) findViewById(R.id.editText);
// Declare and setup "Enter" button
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(new OnClickListener() {
// Call enterClicked() when pressed
#Override
public void onClick(View v) {
enterClicked();
}
});
}
// Sets result to send back to calling Activity and finishes
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
// TODO - Save user provided input from the EditText field
String givenText = mEditText.getText().toString();
// TODO - Create a new intent and save the input from the EditText field as an extra
Intent editText = new Intent();
editText.putExtra("BAG" ,givenText);
// TODO - Set Activity's result with result code RESULT_OK
setResult(RESULT_OK , editText);
// TODO - Finish the Activity
finish();
}
}
I read a lot of posts on Stackoverflow but doesn't work for me, the text not appear.
I try many variants inclusive without last two lines of code from ActivityLoaderActivity.
Purpose of these two lines is not clear in case you want to stay in ActivityLoaderActivity. Just remove them and you'll be fine. This is given that the rest of your code is correct of course.
Intent myIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
startActivityForResult( myIntent, GET_TEXT_REQUEST_CODE );
Alternatively, if you indeed want to restart your ActivityLoaderActivity, you should pass "BAG" extra parameter in the new intent and read it in ActivityLoaderActivity.onCreate():
Intent myIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
myIntent.putExtra("BAG", input);
startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE );
Make sure to declare String input; outside if statements too.
Related
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 8 years ago.
I am trying to write a code in Android to pass a value from a button click to another class. I am getting 0 value in the Log and i am not finding any technique to send the proper value.
Please go through my below code :
Java Code:
MainActivity.java
package com.example.mybuttonvalue;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity
{
Activity activity;
Context cont;
public static int mysum;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,MySum.class);
intent.putExtra("key",5);
startActivityForResult(intent,2);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
}
} }
MySum.java
package com.example.mybuttonvalue;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MySum extends Activity
{
Activity activity;
Context context;
int value = getIntent().getIntExtra("key",0); // 0 is default vlaue
public MySum(Activity activity,Context cont)
{
this.activity = activity;
this.context = cont;
}
public void check(final int x)
{
final Dialog dia = new Dialog(context);
dia.requestWindowFeature(dia.getWindow().FEATURE_NO_TITLE);
dia.setContentView(R.layout.check);
Button btn = (Button) dia.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
int no = 12+x;
myvalue(no);
Intent intent= getIntent();
intent.putExtra("result",x);
setResult(2,intent);
dia.dismiss();
}
});
dia.show();
}
public String myvalue(int x)
{
return String.valueOf(x);
}
}
Log Report
05-18 23:04:29.603: D/My Value :(10375): 0
This is the above code , how can i get 17 in the Log. I should get the result from MySum class to MainActivity class.
Please let me know , suggest me some good solution.
You are instantiating a Activity class public class MySum extends Activity which is wrong
MySum my = new MySum(MainActivity.this,MainActivity.this);
Remove the constructor and the above code.
You need to use intent to pass values between activities. To get value back in MainActivity use startActivityForResult and override onActivityResult and get the value using intent.
Example:
In MainActivity.java
Intent intent = new Intent(MainActivity.this,MySum.class);
intent.putExtra("key",5);
startActivityForResult(intent,2);
Then override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
//get data using intent
int result = data.getIntextra("result",0);
}
}
In MySum.java
int value = getIntent().getIntExtra("key",0); // 0 is default vlaue
Do operation on value
Then to return result
Intent intent= getIntent();
intent.putExtra("result",x);
setResult(2,intent);
Edit:
public class MySum extends Activity
{
#Override
protectes void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mysumlayout); //set layout for mysum
int value = getIntent().getIntExtra("key",0);
check(value);
}
public void check(int x)
{
final Dialog dia = new Dialog(MySum.this);
dia.requestWindowFeature(dia.getWindow().FEATURE_NO_TITLE);
dia.setContentView(R.layout.check);
Button btn = (Button) dia.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
int no = 12+x;
dia.dismiss();
Intent intent= getIntent();
intent.putExtra("result",no);
setResult(2,intent);
finish();
}
});
dia.show();
}
}
Note:
Apart from the above code which is just an example you need not have a Activity at all. All you do is some calculation on a int value which does not require a ui. It could be a normal java class which does just some operation without dialog or any other ui stuff.
I am new to Android an I am doing a tutorial on startActivityForResult. I have search through stackoverflow for a answer but I guess I am doing somthing silly. I have two activities A&B and A starts B and after the user enters some text the activity b is closed and the result is displayed in Activity A. The code is as follows:
Activity A:
package course.labs.intentslab;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ActivityLoaderActivity extends Activity {
static private final int GET_TEXT_REQUEST_CODE = 1;
static private final String URL = "http://www.google.com";
static private final String TAG = "Lab-Intents";
// For use with app chooser
static private final String CHOOSER_TEXT = "Load " + URL + " with:";
// TextView that displays user-entered text from ExplicitlyLoadedActivity runs
private TextView mUserTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loader_activity);
// Get reference to the textView
mUserTextView = (TextView) findViewById(R.id.textView1);
// Declare and setup Explicit Activation button
Button explicitActivationButton = (Button) findViewById(R.id.explicit_activation_button);
explicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startExplicitActivation() when pressed
#Override
public void onClick(View v) {
startExplicitActivation();
}
});
// Declare and setup Implicit Activation button
Button implicitActivationButton = (Button) findViewById(R.id.implicit_activation_button);
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
#Override
public void onClick(View v) {
startImplicitActivation();
}
});
}
// Start the ExplicitlyLoadedActivity
private void startExplicitActivation() {
Log.i(TAG,"Entered startExplicitActivation()");
// TODO - Create a new intent to launch the ExplicitlyLoadedActivity class
Intent eX = new Intent(ActivityLoaderActivity.this,ExplicitlyLoadedActivity.class);
// TODO - Start an Activity using that intent and the request code defined above
startActivityForResult(eX,GET_TEXT_REQUEST_CODE );
}
// Start a Browser Activity to view a web page or its URL
private void startImplicitActivation() {
Log.i(TAG, "Entered startImplicitActivation()");
// TODO - Create a base intent for viewing a URL
// (HINT: second parameter uses parse() from the Uri class)
// TODO - Create a chooser intent, for choosing which Activity
// will carry out the baseIntent. Store the Intent in the
// chooserIntent variable below. HINT: using the Intent class'
// createChooser())
Intent chooserIntent = null;
Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
// TODO - Start the chooser Activity, using the chooser intent
startActivity(chooserIntent);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
// TODO - Process the result only if this method received both a
// RESULT_OK result code and a recognized request code
if (resultCode == Activity.RESULT_OK && requestCode==GET_TEXT_REQUEST_CODE){
// If so, update the Textview showing the user-entered text.
Intent i= getIntent();
if(i!=null)
{
String name = i.getStringExtra("name");
mUserTextView.setText("me"+name);
}
}
}
}
Activity B:
package course.labs.intentslab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ExplicitlyLoadedActivity extends Activity {
static private final String TAG = "Lab-Intents";
private EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.explicitly_loaded_activity);
// Get a reference to the EditText field
mEditText = (EditText) findViewById(R.id.editText);
// Declare and setup "Enter" button
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(new OnClickListener() {
// Call enterClicked() when pressed
#Override
public void onClick(View v) {
enterClicked();
}
});
}
// Sets result to send back to calling Activity and finishes
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
// TODO - Save user provided input from the EditText field
String userText = mEditText.getText().toString();
// TODO - Create a new intent and save the input from the EditText field as an extra
Intent i = new Intent();
i.putExtra("name", userText);
// TODO - Set Activity's result with result code RESULT_OK
setResult(RESULT_OK, i);
// TODO - Finish the Activity
finish();
}
}
Can someone please spot my error,
Cheers,
Den
Intent intent = new Intent(actuallActivity.this, ActivityToBeLaunch.class);
intent.putExtra("name", userText);
actuallActivity.this.startActivity(intent);
I found the problem... I was creating an intent in resultActivity but not assigning the returned intent to the new intent." The line that fixed it is Intent i = data;" Thanks again, sometime you just need to talk it out with someone..
I'm trying to use a barcode scanner and then take that input and use in another activity to open with a url. I've been able to get the data to return, just not in another activity and haven't seen any projects exactly like this. I'm not sure if it has to do with intent or how I'm calling the string. The webview in the second java works but doesn't take the string. Thanks for the help!
Scanner.java (which works okay)
package com.pangolin.rollin.ts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Scanner extends Activity {
TextView tvStatus;
TextView tvResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
Button websku = (Button) findViewById(R.id.btnsku);
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
startActivity(myintent);
}
});
tvStatus = (TextView) findViewById(R.id.tvStatus);
tvResult = (TextView) findViewById(R.id.tvResult);
Button scanBtn = (Button) findViewById(R.id.btnScan);
// in some trigger function e.g. button press within your code you
// should add:
scanBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, Toast.LENGTH_LONG)
.show();
}
}
});
}
// In the same activity you’ll need the following to retrieve the results:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
tvStatus.setText("Press a button to start a scan.");
tvResult.setText("Scan cancelled.");
}
}
}
}
And websku.java (doesn't work, supposed to take results from previous activity.
package com.pangolin.rollin.ts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Websku extends Activity {
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String sku = intent.getStringExtra("SCAN_RESULT");
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_websku);
WebView webView = (WebView) findViewById(R.id.webview_sku);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.title_activity_websku);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://m.radioshack.com/radioshack/catalog/searchList.do?categoryId=&keyword="+sku);
};
}
You don't set any extra to Websku intent:
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
startActivity(myintent);
}
});
Should be:
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
myintent.putExtra("somename", somevalue);
startActivity(myintent);
}
});
You don't set the extras for the websku Activity. Save the intent returned from the scanner:
private Intent mWebskuIntent;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
mWebskuIntent = intent;
// more of your code
Then when you start the websku Activity make a copy of the saved intent which will copy also the extras returned from the scanner:
Intent myintent = new Intent(mWebskuIntent);
myintent.setClass(Scanner.this, Websku.class);
startActivity(myintent);
You might want to check for mWebskuIntent being null as well.
I want the values of edit text restored when user comes back to my first activity?
Please help me out.
Thanks in advance
this is my first activity code for getting user values in edit text
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
/** Called when the activity is first created. */
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ed1_val = Float.parseFloat(ed1.getText().toString());
ed2_val = Float.parseFloat(ed2.getText().toString());
Log.v("TAG", "inside saved instance");
savedInstanceState.putFloat("ed1", +ed1_val);
savedInstanceState.putFloat("ed2", +ed2_val);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v("TAG", "inside on restore");
float ed_val = savedInstanceState.getFloat("ed1");
float ed2_val = savedInstanceState.getFloat("ed2");
ed1.setText("" + ed_val);
ed2.setText("" + ed2_val);
}
}
this is my second activity code
public class Second_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
IntentActivity.class);
startActivity(intent);
}
});
}
}
It is not a good idea to start the first activity again on back pressed. Call finish() in the second activity. This will lead to the resume of the first activity which is what you need.
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish(); }
});
}
You don't need neither onSaveInstanceState nor onRestoreInstanceState.
Just call finish in the onClick listener for the button in the second Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
}
This is the second one:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
That way you are resuming the previous Activity instead of starting new one.
If you need to pass data between them you could use startActivityForResult / onActivityResult and setResult methods:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
private static final int GET_VALUES_REQUEST_ID = 1;
public static final String FIRST_VALUE_ID = "first_value_id";
public static final String SECOND_VALUE_ID = "second_value_id";
private static final float DEFAULT_VALUE = 0;
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivityForResult(intent, GET_VALUES_REQUEST_ID);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case GET_VALUES_REQUEST_ID: {
if (Activity.RESULT_OK == resultCode) {
ed1_val = data.getFloatExtra(FIRST_VALUE_ID, DEFAULT_VALUE);
ed2_val = data.getFloatExtra(SECOND_VALUE_ID, DEFAULT_VALUE);
setValues();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void setValues() {
ed1.setText(Float.toString(ed1_val));
ed2.setText(Float.toString(ed2_val));
}
}
The second activity could be something like that:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent data = new Intent();
data.putExtra(IntentActivity.FIRST_VALUE_ID, 324f);
data.putExtra(IntentActivity.SECOND_VALUE_ID, 32234f);
setResult(Activity.RESULT_OK, data);
finish();
}
});
}
}
This is a very basic example so I just hardcoded some return values - please implement something more meaningful.
Beside that you could avoid using underscores as word separator in class names - camel case is much more accepted as name convention.
just finish the second activity don't start it via intent.
When you finish the second activity first activity will be automatically resumed,
you can go for overriding protected void onSaveInstanceState(Bundle outState) and protected void onRestoreInstanceState(Bundle savedInstanceState)
Here is an example
Remember it will work when you will not finish your previous activity.Do this in your first activity.
You are starting a new activity by pressing back button on Second Activity. The new activity instance is not the previous one that has started the Second Activity hence onRestoreInstanceState callback is not getting called.
Its easy if you do not need to send data back to the first activity. How do send back data without using an intent.
I would like to make sure that when user log in it will stay in session no matter what happens (crashed, shut down/power down/reboot, leaving the app) at same time the user info data will be sending with all the activities in the app to the webserver.
for example at the start up of the app, user login '9999' it goes to the main activity that have 5 diff. activities. user 9999 will send one activity (i.e. gps location) it will send that info to the webserver as user 9999 gps 123.234 123.123.
I want to ensure the users stays in session and also send its users data with the "activity" data sent.
I read this link
What is the most appropriate way to store user settings in Android application
I was still unable to put it together.
At the same time in the same main screen it has a logout. User needs manager approval to logout by inputting the code(i.e. 1234) to completely logout and for new user to input their id number. I want to know how to put the hardcode '1234' within the activity.
this code is my main screen after login to give you the idea
MainActivity.java
import android.app.ListActivity;
import android.content.Intent; import
android.os.Bundle; import
android.view.View; import
android.widget.ArrayAdapter; import
android.widget.ListView; import
android.widget.TextView;
public class Customer extends ListActivity {TextView selection;
CustomerListItem[] items ={
new CustomerListItem("Start Trip",StartTripActivity.class),
new CustomerListItem("Clock in",ClockinActivity.class),
new CustomerListItem("Customer Svc",CustomerSvcActivity.class),
new CustomerListItem("IndependentInspection",InspectionActivity.class),
new CustomerListItem("Pick Up", PickUpActivity.class),
new CustomerListItem("Log Out", LogoutActivity.class)};
private TextView resultsTxt;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<CustomerListItem>(
this, android.R.layout.simple_list_item_1,
items));
selection = (TextView) findViewById(R.id.selection);
}
#Override
protected void onListItemClick(ListView l, View v,
int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent(this,
items[position].getActivity());
startActivityForResult(intent, position);
}
#Override
protected void onActivityResult(int requestCode, int
resultCode, Intent intent)
{
super.onActivityResult(requestCode,
resultCode, intent);
if (resultCode == RESULT_OK)
{
// Perform different actions based on from which activity is
// the application returning:
switch (requestCode)
{
case 0:
// TODO: handle the return of the StartTripActivity
break;
case 1:
// TODO: handle the return of the ClockinActivity
break;
case 2:
// TODO: handle the return of the CustomerSvcActivity
case 3:
// TODO: handle the return of the InspectionActivity
break;
case 4:
// TODO: handle the return of the PickUpActivity
break;
case 5:
// TODO: handle the return of the LogoutActivity
break;
default:
break;
}
}
else if (resultCode == RESULT_CANCELED)
{
resultsTxt.setText("Canceled");
}
} }
UPDATE:
Login.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends Activity {
private EditText etUsername;
private Button btnLogin;
private Button btnCancel;
private TextView lblResult;
/** Called when the activity is first created. */
//#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etUsername = (EditText)findViewById(R.id.username);
btnLogin = (Button)findViewById(R.id.login_button);
btnCancel = (Button)findViewById(R.id.cancel_button);
lblResult = (TextView)findViewById(R.id.result);
btnLogin.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
if(username.equals("guest")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);
} else {
lblResult.setText("Login failed. Username doesn't match.");
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
// Close the application
finish();
}
});
}
}
The link you included shows the way to store the user's ID - you can use SharedPreferences or you can store it in the database.
You can store the "approval code" anywhere. If you want to hard-code it, you may want to put it in a "static" helper class in a public static final String variable.