In my manifest.xml, i'm used android:parentActivityName to set up parent Activity.
Here my code:
<activity
android:name=".B"
android:label="#string/title_activity_A"
android:parentActivityName=".A" >
</activity>
So, how to put data from B to A when click back button in action bar??
Because my A activity is child activity of another activity(C activity) and A activity using data from this activity(C activity)...
Somebody can help me please? Thanks and sorry because my english.
In your Activity B class write:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent intent= new Intent();
intent.putExtra("param", "value");
setResult(RESULT_OK, intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In Activity A class add:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == 2404) {
if(data != null) {
String value = data.getStringExtra("param");
}
}
}
Start an Activity B using startActivityForResult Method
Intent intent = new Intent(A.this.getApplicationContext(), B.class);
startActivityForResult(intent, 2404);
You have to Override the actionbar back button click event & set data to parent activity with Intent parsing & handle in parent activity in onActivityResult() method.
Related
I have onClick method in MainActivity as following,
public void onClick(View view) {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I", "0");
startActivity(i);
}
In SecondActivity I validated the Intent as following
if (getIntent().getStringExtra("I") == null) {
Toast.makeText(this, "NULL", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "NOT NULL", Toast.LENGTH_LONG).show();
}
and started the ThirdActivity from SecondActivity as following
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivity(i);
}
Now when come back to SecondActivity from ThirdActivity getIntent().getStringExtra("I") returns null. I also tried overriding onOptionsItemSelected as following.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = getIntent();
i.putExtra("I", "0");
break;
}
return super.onOptionsItemSelected(item);
}
Now don't know how to fix this issue. I don't want to use SharedPreference for this issue.
Please follow these steps:
start ThirdActivity for result
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);
}
Update following method, add setResult(Activity.RESULT_OK, i);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = getIntent();
i.putExtra("I", "0");
setResult(Activity.RESULT_OK, i);
break;
}
return super.onOptionsItemSelected(item);
}
In SecondActivity, implement onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1 && resultCode == RESULT_OK) {
// Now use data.getStringExtra("I");
}
}
You are not finishing any activity, and you are passing value from main activity to second, then calling third activity, just returning from the third activity and resuming the second activity, you are not passing any data to second activity while coming from third activity.
just change your code as follow:-
//from second to third
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivity(i);
finish();
}
// returning from third to second
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = new Intent(this, second.class);
i.putextra("I","0");
startActivity(i);
finish();
break;
}
return super.onOptionsItemSelected(item);
}
also you can change like this:-
#Override
public void onBackPressed() {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I","0");
startActivity(i);
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
in your third activity only
if you dont want to startactivty then startActivtyForResult() from second to third:-
startActivityForResult(i,101);
implement onActivtyResult method in second activity:-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 101 && resultCode == RESULT_OK) {
// Now use data.getStringExtra("I");
}
}
for third activity just finish it in onBackpress() like:-
#Override
public void onBackPressed() {
Intent resultData = new Intent();
resultData.putExtra("I", "0");
setResult(Activity.RESULT_OK, resultData);
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
This is because when you come back from third activity to second activity, getIntent() returns the Intent of third activity and not the first.
And while navigating from third activity to second, you are not passing any string extra and hence you get a null value.
You can try using startActivityForResult() method for your linking from SecondActivity to ThirdActivity this way, you can then use setResult(intent) before killing the ThirdActivty. Thus when you come back to your SecondActivity you will have the result intent you passed in your ThirdActivty inside the onActivityResult method of your SecondActivity.
Hope this helps.
Make a change when you move towards SecondActivity from ThirdActivity.:
Intent i = new Intent(this, SeconActivity.class);
i.putExtra("I",getIntent().getStringExtra("I"));
startActivity(i);
you have to pass again
i.putExtra("I", "0");
from third activity to second activity
try this :
put this on your thirdactivity
#Override
public void onBackPressed() {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I","0");
startActivity(i);
finish();
}
I have an Activity A and Activity B and the problem is I'm unable to call the onActivityResult in Activity A from B using menu option.
Now this is how I go to Activity A from B
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return true;
case R.id.category_add:
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now in Activity B I perform some operations and get back to Activity A as shown
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return true;
case R.id.task_add:
Intent intent = new Intent(this, ActivityA.class);
startActivity(intent );
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now in Activity A onActivityResult which is not calling:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == 1) && (resultCode == Activity.RESULT_OK)) {
if (data != null) {
sampledata= data.getStringExtra("sampletext");
}
}
}
This is my Manifest file:
<activity
android:name="com.sample.example.ActivityA"
android:label="#string/title_sample_app"
android:parentActivityName=".MainActivity"
android:theme="#style/Theme.Default" >
</activity>
<activity
android:name=".ActivityB"
android:label="#string/title_activity_login"
android:theme="#style/Theme.Default" >
</activity>
The problem is you're starting an Activity again from Activity B.
The correct way (if you started an Activity for result) is to finish it like this:
Intent resultIntent = new Intent();
setResult(RESULT_OK, resultIntent);
finish();
Some more Information:
To learn more about starting Activities (for result), read: Starting Activities and Getting Results
To learn more about getting results from other apps (could be interesting, too), read: Interacting with Other Apps - Getting a Result from an Activity
To return back to ActivityA, you don't want to start it again but to finish ActivityB
case R.id.task_add:
Intent intent = new Intent();
intent.putExtras( whatever you need to pass back to A);
setResult(Activity.RESULT_OK, intent);
finish();
break
check for typo
I'm having a problem in my android application. I don't know why 'onActivityResult' method is not being called when 'Up navigation' button from action bar is pressed. I think I've done everything properly:
Parent activity launch child activity with 'startActivityForResult' method.
Intent intent = new Intent(ParentActivity.this, ChildActivity.class);
startActivityForResult(intent, 1000);
Parent activity has overridden 'onActivityResult' method.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (data != null && requestCode == 1000)
{
Bundle extras = data.getExtras();
Boolean rc = extras.getBoolean(MyConstants.INTENT_EXTRA_RESULT);
if (rc)
{
.......
}
}
}
Child activity has overridden 'onOptionsItemSelected' and calls 'NavUtils.navigateUpFromSameTask'.
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home)
{
Intent result = new Intent((String)null);
result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
setResult(RESULT_OK, result);
NavUtils.navigateUpFromSameTask(this);
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
Child activity has overriden 'finish' method. This method set a result.
public void finish()
{
Intent result = new Intent((String)null);
result.putExtra(Constantes.INTENT_EXTRA_HAY_QUE_RECALCULAR, hayQueRecalcular);
setResult(RESULT_OK, result);
super.finish();
}
I'm not sure why 'onActivityResult' method is not being called.
What I've observed is that child activity is not being finished ('finish' method is not being called) when 'Up navigation' button from action bar is pressed. However it is called when back button (hardware button) is pressed.
What I'm doing wrong?
Thanks
As your child Activity is just on the top of your Parent Activity, no need of this method
NavUtils.navigateUpFromSameTask(this);
write like
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent result = new Intent((String) null);
result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
setResult(RESULT_OK, result);
finish();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
finish your Child Activity when home button is pressed.
I have one MainActivity and I have defined below code in onCreate() method. The intention is, when MainActivity gets extra String "EXIT" then show Toast message:
Intent current = getIntent();
if (current !=null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
This MainActivity starts another activity "DayOne" on some button press like:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.this.startActivity(i);
}
Now I am returning back from "DayOne" to MainActivity after putting extra string "EXIT". This I am doing inside onOptionsItemSelected(MenuItem item) method:
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
The issue is, when MainActivity is getting called from DayOne with extra string "EXIT"; I am not seeing the Toast message defined in MainActivity. What is missing or wrong here?
Appreciate any help.
Thanks all for your comments and helps.
I have figured out the issue here. It was because the manifest file had entry an entry android:launchMode="singleInstance" for Both the activities (MainActivity and DayOne Activity)..
Removing it from there, it worked fine.
First check your null then get the Intent String, can you just make your code look like below within in onCreate of MainActivity#
Bundle extra= getIntent().getExtras();
if(extra!=null){
String _StrExit=extra.getString("EXIT");
if(_StrExit.equalsIgnoreCase("EXIT")){
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
Update
Make change while calling Intent from menuitem
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String _Str="EXIT";
i.putExtra("EXIT", _Str);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
Its working for me.
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent current = getIntent();
if (current != null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
public void processGo(View view) {
Intent i = new Intent(MainActivity.this, OneDayActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
OneDayActivity:
public class OneDayActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_one);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.day_one, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals("Exit")) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
dont clear the stack instead of if you want to do some task when you come back to your main activity (using "EXIT" string) you can achieve that using onActivityResult also.:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
MainActivity.this.startActivity(i,10); // 10 is the id to handle
}
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data )
{
super.onActivityResult( requestCode, resultCode, data );
switch ( requestCode )
{
case ( 10 ): // id that we pass on start activity
{
if ( resultCode == Activity.RESULT_OK )
{
Toast.makeText(this, "exiting"+data.getStringExtra( "EXIT", "" ), Toast.LENGTH_LONG).show();
}
}
break;
}
}
On finish
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent resultIntent = new Intent();
resultIntent.putExtra( "EXIT", "EXIT" );
setResult( Activity.RESULT_OK, resultIntent );
finish();
}
return super.onOptionsItemSelected(item);
}
on finish it will go back to Main Activity and call onActivityResult automatically where you can do your task.
Sorry for the typo hope it will help.
I have a popup activity window (using the inflater) and an array
public class PopUp extends Activity{
ArrayList<String> nameSave = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_layout);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.popup_layout, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void upload(View view)
{
EditText editText = (EditText) findViewById(R.id.nameBox);
String name = editText.getText().toString();
nameSave.add(name);
}
First of all I'm not even sure if I got the array/list working right. Basically when the person hits the button 'upload' I want it to grab the name and add it to the array. Then I want that array sent back to the previous activity (Launch) which has 2 fragments. One fragment is a listview and I want to put the array's content into that fragment. How do I go about passing this array? Is it as simple as putting it in an intent?
Yes you can put putStringArrayListExtra(name, value) in your intent pass the intent by startActivityForResult(intent, requestCode) and when you done this Activity. You can get the intent by the method described below because the method will be called when your current Activity finished. So get the required data and do what ever you want to do with this data. Or you can also put the array in the intent.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
get your data here and do what ever you want to do with it
}
}
in previous Activity. My be this will help you.