I am attempting to pass an instance of a serializable class to a parent activity when the user clicks on the up navigation button in the support action bar. The activity in which I am having trouble was started with startActivityForResult() so I can successfuly pass the instance through an intent with the onBackPressed() method overwritten like this
#Override
public void onBackPressed() {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("world_key", world);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
but I am lost when attempting to run the same code when the user clicks on the up navigation on the support action bar in my activity
for extra clarity on what I am trying at the moment, I have tried the following, which has not worked
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toolbar:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("world_key", world);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
return super.onOptionsItemSelected(item);
}
And insight would be greatly appreciated
You need to use android.R.id.home like the following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// send bundle here.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Related
I am a beginner, I am a few days with this problem
I didn't find a solution. I have a menu that appears in an activity and when I click, I want it to open a new activity.
My question is, what to put in the activity with menu, and what to put in the new activity?
This is my code
Menu_chat.xml (my menu)
android:id="#+id/salva_vida"
android:icon="#drawable/salva_vida"
android:title="#string/save_life"
app:showAsAction="always" />
ChatActivity.java (this is the activity with menu)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
??????? (What put here?)------------------
break;
tab2.java (this is the new activity- I want to open this)
public class tab2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab2);
}
}
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
Your ChatActivity.java will look like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
//Start Activity here
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
break;
Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent);
#Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case android.R.id.home: onBackPressed(); return true; case R.id.salva_vida: //Start Activity here Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent); break;
Don't forget to add your tab2 activity to manifest.
I'm converting some activities to several fragments , and now when I press back button it doesn't work.
what changes should I do in this fragment when its returning back to Previous activity and when returning to Previous fragment?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity().getApplicationContext(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try like this
write this in onCreateView() method
setHasOptionsMenu(true)
and make these changes
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It may help. If this also doesn't works then you have to manage fragment backstack in activity from where you are calling your fragment
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 that can be asked to run after clicking buttons on many different activities and hence it does not have a "single parent". Therefore in the android manifest I cannot define its parent so I cant get the "Up" button to function properly.
Is there a way I can have the "up" button return to the activity that called it?
You can pass ComponentName of starting activity as an extra
intent = new Intent(this, UpButtonActivity.class);
intent.putExtra(EXTRA_PARENT_COMPONENT_NAME, new ComponentName(this, ThisActivity.class));
startActivity(intent);
The Activity with up button
private ComponentName parent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parent = getIntent().getParcelable(EXTRA_PARENT_COMPONENT_NAME);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getId()) {
case android.R.id.home:
if (parent != null) {
final Intent parentIntent = new Intent();
parentIntent.setComponentName(parent);
startActivity(parentIntent);
finish();
return true;
} else {
return super.onMenuItemSelected(featureId, item);
}
//...
}
}
Here is my problem:
Create a MainActivity. Add a button which will start another activity SecondActivity.
Intent i = new Intent(getActivity(),SecondActivity.class);
startActivityForResult(i,0);
Inside the SecondActivity, I capture the back button click event and also add a button to return to the first Activity.
When back button in action bar is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
//finish();
return false;
}
return super.onOptionsItemSelected(item);
}
When the button inside activity is clicked:
Button btn = (Button)this.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
The onActivityResult in MainActivity is called when I click the button inside the SecondActivity, but it's never been called if I click the back button in Actionbar of SecondActivity. Can anybody tell me why? Thanks
Here is the code which is working:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I guess the finish() will close the current Activity, and return true inform that action has been processed. (The default back action seems to be different from finish().)
Try this:-
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Good answer is Gopal Rao code in the same question. Its worked for me. This is a copy of his solution:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent result = new Intent((String) null);
result.putExtra("SOME_CONSTANT_NAME", true);
setResult(RESULT_OK, result);
finish();
return true;
}
else {
return super.onOptionsItemSelected(item);
}
}