How to pass Parcelable object from child to parent activity? - android

I'm building a small app that requiere authentication. In my main activity I have a Parcelable class named "user" that contains the username and password of an user, when a user click on a button it starts a new activity passing that user class. It works like a charm, in the child activity the user fill the form to authenticate, then when user press the back button, I would like to send the "user" class back to my main activity.
Is it possible to do that ??

Start your child activity with:
startActivityForResult(startIntent, 1);
In your child activity, intercept the back button and append your data:
#Override
public void onBackPressed() {
Intent data = new Intent();
data.putExtra("key", yourDataHere);
setResult(Activity.RESULT_OK, data);
super.onBackPressed();
}
And get data inside parent activity inside onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
DataType yourData = (DataType) data.getParcelableExtra("key");
//Do whatever you want with yourData
}
}

Related

saving the data that is passed through intents [duplicate]

This question already has an answer here:
Android: Saving and Restoring data when switching between Activities
(1 answer)
Closed 5 years ago.
I have a TextView and a Button in my first activity and an EditText and a Button in my second Activity.when i click a button in my first activity it must be redirected to second Activity.In the second activity i want to give the data in the EditText and click the button it must be redirected to first activity and second Activity EditText value must be displayed in the TextView given in the first activity.my problem is i want to save the EditText data after returning back from the first activity.
public class ActivityA extends Activity {
private void gotoActivityB(){
startActivityForResult(new Intent(this,ActivityB.class),101);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 {
if (resultCode == RESULT_OK) {
String value = data.getStringExtra("KEY");
}
}
}
}
public class ActivityB extends Activity {
#Override
protected void onDestroy() {
super.onDestroy();
Intent intent = new Intent();
intent.putExtra("KEY",editTextValue);
setResult(Activity.RESULT_OK,intent);
}
}

How to startactivity for result from many activity to the same activity?

i use DrawerLayout to create my project and then my project is allow user to login anytime as they want. so how can i start activity for result from each fragment to the same new activity
By looking at your question, I assume your DrawerLayout is inside an Activity, correct?
If that so, the way you are changing fragment is by targetting a View inside the Activity correct?
Then, just do getActivity().startActivityForResult(yourIntent, yourRequestCode)
and in your Activity's onActivityResult you can do something like :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == yourRequestCode) {
// Your code here, you can check the resultCode from here too, and data
// is the intent sent from
// getActivity().startActivityForResult(yourIntent, yourRequestCode)
}
A little addition, you can also startActivityForResult from inside a Fragment to get result in the Fragment too for better understanding and handling depending on your code, simply by removing getActivity()
So it'll be like startActivityForResult(yourIntent, yourRequestCode)

How to get access to back button in android?

Can you please clarify me, what's the best practice for this example:
Activity1 does INSERTS on SQLite and starts Activity2.
On Activity2, the user clicks on Back button and goes back to Activity1.
But instead of making INSERT, I want to make UPDATE to the data on SQLite.
How do I control, that user "came" back?
What's the best practice?
Assume on button click you starting Activity 2
public static final int YOURCONSTANTNUMBER = 77;
//Button Value
Button second = (Button)findViewById(R.id.firstButton);
second.setOnClickListner(this);
#Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.btnSecondActivity:
//Code for inserting data into database
Intent secondactivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(secondactivity);
break;
}
}
In your second activity
To take control over the back button use the below code
#Override
public void onBackPressed()
{
//Code for update
}
EDIT
Call your second activity with startActivityForResult() method which will let you know the result.
In First Activity
Intent secondactivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(secondactivity,YOURCONSTANTNUMBER);
Getting Result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case YOURCONSTANTNUMBER:
// It will execute if you returning from second activity
//Update you database here
}
}
In Your Second Activity
Call this method were appropriate setResult(YOURCONSTANTNUMBER);
Hope it helps
Assuming you want to do both DB operations in Activity1, consider startActivityForResult() here:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)

restart activity in android

I have one activity A, that has one button and one list view which shows names of books . on click of the button, activity B starts, there user fill the book form and save it . when he press back button , user comes to activity A. Here the book name should be updated in listview. I think I have to write some code in onResume() . Can u please tell me what to write. I am using customised list view.
Start activity B with startActivityForResult() and use method onActivityResult() to restart or process the new data
For example, to start Activity B:
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
Then somewhere in your Activity A class:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
// do processing here
}
}
The other answers should suffice, but onResume() can be called in cases where the activity is resumed by other means.
To simply restart Activity A when user presses back button from Activity B, then put the following inside the onActivityResult:
if(requestCode == 0){
finish();
startActivity(starterintent);
}
And in the onCreate of Activity A, add starterintent = getIntent();
Just remember to initiate the variable with Intent starterintent; somewhere before your onCreate is called.
e.g.
public class ActivityA extends ListActivity {
Intent starterintent;
public void onCreate(Bundle b){
starterintent = getIntent();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
finish();
startActivity(starterintent);
}
}
private void startActivityB(){
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
}
}
Then just call startActivityB() from a button click or whatever
YES you are right. Write code in onResume.
When you updated date just call notifyDataSetChanged(); for your ListView adapter
Hope, it help you!
You can either start the activity when user press on Save, and it will fix it for you.
Or if you want to press back:
#Override
public void onResume(){
super.onResume();
list.clear();
list.addAll(getBooks());
adapter.notifyDataSetChanged();
}

I am trying to create a properties page in Android

I need some help creating a properties page in Android.
I have created an Activity and have a TableLayout with a title, 5 properties that include a Spinner to select a property class and a EditText that the user can type into, followed by a couple of buttons reading Accept and Cancel.
I am launching the activity by creating a new Intent with that class and using a StartActivityForResult, where I need to Bundle all of the properties back to my main activity. I have an OnActivityResult defined in the main class. I am having difficulty in the properties page on how to return the activity result from the button callback and how to package and return the Bundle. Any not so simple examples would be appreciated.
on the Activity that is returning the result you would do something like this
Intent i = new Intent();
i.putExtra("value_a", someValue);
i.putExtra("value_b", anothervalue);
setResult(RESULT_OK, i);
finish();
then in the activity that is waiting for the result you would do something like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) // 0 would be whatever id you gave this when you started the activity for result
{
Bundle extras = data.getExtras();
String property_a = extras.getString("value_a");
String property_b = extras.getString("value_b");
// ... whatever else you need to with the results, maybe they are not strings...??
}
}
You can use Preference Activity to store yours application settings

Categories

Resources