How to open intent and add value android - android

I have trouble with returning a value to another intent in android.
This is my situation:
I have Activity A, which is opened when I start my application.
When I click on a button I start Activity B.
In activity B I fill in a EditText and return a string.
Then I click on a button in activity B to return to activity A.
I don't know what method I should use in activity A to use the value I get returned from activity B.

this kind of situation fits good for startActivityForResult and onActivityForResult. When you press on the button, instead of calling startActivity you call startActivityForResult in order to start ActivityB. In ActivityB when you click on the button, you have to fill up an Intent with the values you want to return to ActivityA and, in order, call setResult(result, Intent) and finish(). The onActivityResult of ActivityA will receive the intent with your data
Activity Docs

You should start activity B by calling startActivityForResult() instead of startActivity() and on Activity B, in your exit button click listener, setResult() results to pass to Activity A.
You can handle the result by overriding onActivityResult() method in Activity A
Check this : http://developer.android.com/reference/android/app/activity.html
Search for "Starting Activities and Getting Results"

Related

Android - How do I switch between 1st and 3rd activities?

I have a 1st and a 3rd activity. I wish to push a button on the 3rd activity and it finishes the 3rd and 2nd activity switching all the way back to the 1st activity(it's the MainActivity might I add). In other words, is there a way to finish automaticly the call stack of activities to a specific activity?
You can use method startActivityForResult() to start your activity.
You need to call method setResult() when you finish the activity.
And override method onActivityResult() to do what you want to do.
Actually, seems 2 methods can solve this.
Assuming Activities: A -> B -> C, and all alive in the same stack.
1) Use android:launchMode="singleTask" for Activity A. When C calls A, A starts and both B & C finish.
2) When starting A, adding flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
if you don't have to pass any data to the main activity, i guess this is what you are looking for: how back to the main activity in android?
execute this code at the button click in your 3rd activity:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this code clears your activity stack and reopens your main activity.

Android, EditText preservation through new activities

How can I preserve data between activities? For instance, I have an edit text and a button in Activity A. I fill in that editText and then click on the button. That button starts a new intent, Activity B. In B there are several buttons, but all of them if clicked on, start activity A again. I want that editText to be still filled in as well as pass specific data to Activity A depending on the button pressed in Activity B.
I know this has to do with onStop() -> onStart(), but couldn't get it to work.
You should start Activity B with startActivityForResult. Then, when u set the result, you should "catch" it in Activity A by overriding onActivityResult().
As for EditText, if you don't call finish() on Activity A - you don't need to override onSaveInstanceState or anything else - system does it for you.
Don't finish the activity in Activity A intent to Activity B. and use startActivityForResult(intent) rather than startActivity(intent) to be able to pass the data back to activity A again. check this answer to how How to manage `startActivityForResult` on Android?.

Activity calling other activities on Android sequence

How can I save and remember the activity that got launched by a specific activity? Say I have activity 1 which launches activity 2 using startActivityForResult().
Then I see the following sequence of calls:
onPause called from Activity1!
onSaveInstanceState called from Activity1
onActivityResult called from Activity1
Later when I hit the back button to go back to Activity1 from Activity2, I see the onRestart called:
onRestart called from Activity1
onResume called from Activity1
So my question now is how do I identify that the transition is from Activity2 -> Activity1 rather than (say) Activity3 -> Activity1?
One possible way is to use startActivityFprResult instead. When you finish any of your other Activities (in this case Activity2 or Activity3), you call setResult(RESULT_OK, intent) and provide an Intent. This will be delivered to Activity1 in onActivityResult, and you can just put some extra in the Intent to identify which Activity just finished.

Android: How to pass back key to the activity behind

All:
Is there a way that can pass back key to the activity behind?(Two activities A,B; B is on top of A; How to let B ignore back key and let A receive back key?)
Thanks a lot!
BR.
use StartActivityForResult(Intent, int) if you start Activity B from Activity A.
Then in B: set the result on the Intent through getIntent() and finish() activity B
then override onActivityResult in Activity A to get the value
start activity B with following flags added to that intent.
intent.setflags(INTENT.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent);
then your back will be the back from your previous activity.
Probably you might exit the app if there are no activities behind A
or another simple solution is,
call finish() after you call startActivity(...) for B in A.

Android finish parent Activity

I have 3 different activities in a TabGroupActivity. Let's say A - Parent, B - Child 1, C - Child 2.
A --- starts --> B
B --- starts --> C
and I have an alert dialog in C which shows some message. I want to go back to activity A when I press Ok button on dialog.
But the problem is that I can open activity C from other activities too and I want to go back to their parent activities too. So I need to make something which will work no matter which activity opens C. I've tries with this one but didn't work :
Intent intent = new Intent(Synchronization.this,Synchronization.this.getParent().getClass());
but it didn't help me. Any suggestions?
You just have to make use of two Activity methods viz. startActivityForResult() and onActivityResult()
Example : http://www.vogella.de/articles/AndroidIntent/article.html#explicitintents
Here goes the logic :
In ActivityB
Start ActivityC by using startActivityForResult(activityCIntent,INT_CODE);
In ActivityC
Now check if Dialog's OK Button is pressed, if yes then set the result using setResult(RESULT_OK,intent); and then call finish();
Then control will be redirected to ActivityB's onActivityResult() method.
Now inside onActivityMethod() check whether result_code==RESULT_OK and requestCode = INT_CODE. If yes then simply call finish();
In the Activity B start the C Activity as startActivityForResult() so when you finish the C activity it will back to the B with result. As the result you may pass flag with the intent object.
Now when you finish the C activity with ok button then set the result as RESULT_OK into the setResult() if you need to pass the data back to the B activity you may set the data into the Intent add this intent with the setResult() method and then finish the C Activity.
Now in B check the requestcode is from the C then finish this Activity. As you start this C Activity you can also start the B Activity for the A Activity.
And you need to override onActivityResult() in B activity and if you start the B Activity as for result then also you need to define also into A Activity

Categories

Resources