Im able to send data to parent activity when back button is pressed.
However, for up button i cannot implement similar logic and data is not getting returned to parent activity why is that ?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
.....
Intent resultIntent = new Intent();
resultIntent.putExtra("noteJSON", noteJSON.toString());
setResult(Activity.RESULT_OK, resultIntent);
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Im using startActivityForResult to start childactivity. On onactivityresult basically handling the intent info. When back, button is pressed its working. When UP is pressed onactivityresult never gets executed. Thats the problem
When you call navigateUpFromSameTask method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward. As a result onActivityResult won't be called.
If you want to ensure result is returned via onActivityResult then remove call to navigateUpFromSameTask as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
....
Intent resultIntent = new Intent();
resultIntent.putExtra("noteJSON", noteJSON.toString());
setResult(Activity.RESULT_OK, resultIntent);
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
try below
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
.....
startActivity(new Intent(YourActivity.this,ParentActivity.class).putExtra("noteJSON", noteJSON.toString()));
return true;
Related
I have made an app with many activities and I have included a bottom navigation bar to all of them. I used finish(); after any intent not to get any memory leak problem but when I press the back button of my phone the app closes. What should I do?
//an intent of my app
Button btnsuita = (Button) findViewById(R.id.souita_btn);
btnsuita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(HotelRooms.this, HotelRoomsSouita.class);
startActivity(intent);
finish();
}
});
//An Example of Bottom Navigation Bar
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.BottomNavView_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
android.view.MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_home:
Intent intent1 = new Intent(HotelRoomsSouita.this, MainMenu.class);
startActivity(intent1);
finish();
break;
case R.id.ic_back:
Intent intent2 = new Intent(HotelRoomsSouita.this, HotelRooms.class);
startActivity(intent2);
finish();
break;
case R.id.ic_contact_us:
Intent intent3 = new Intent(HotelRoomsSouita.this, Mail.class);
startActivity(intent3);
finish();
break;
}
return false;
}
});
Remove finish() in the switch every case.
If you want to ignore back button clicks, then you can add this code in the activity.
#Override
public void onBackPressed() {
//Do anything if you wish
}
Since you are finishing every activity ,there is o activity remain in the backstack or background . so the application closes.
You can use view pager to inflate fragment for bottom navigation view . This is the correct approach by google material design guid lines.
Never use activity for bottom navigation view .
And still if you continue with your activity , then override the onBackpress method of the activtiy .
If I understand your problem correctly you want the user to be able to go back in previous activities while your application does not cause any memory leaks(by starting same activity again and again)
Solution:
Remove finish() from each case in onNavigationItemSelected
Go to your manifest file and add android:launchMode="singleInstance" to each Activity
Ovveride onBackPressed so that it does nothing in your first-started activity as dcanh121 said.
If you do that then if user tries to open the same Activity which was already opened before, it will cause the app to open the previous activity instead of creating a new one.. (read more about this)
Therefore you won't have any memory leak problems :)
I've got 2 activity's; A.java and B.java.
I want to have an Action Bar Up Button in the B.java, when I execute this code:
startActivity(new Intent(A.this, B.class));
finish();
And I create the Up Button in activity B.java with this code:
ActionBar ab = new ActionBar();
ab.setDisplayHomeAsUpEnabled(true);
And I also set the parentActivityName for activity B in the Android Manifest to:
android:minSdkVersion="16"
...
android:parentActivityName="com.name.appname.A"
My up button appears, but when I click it, the application just finishes himself instead of going back to activity A.java.
I think this is because of the "finish();" when I call activity B, am I right?
Can anybody help me?
Thanks in advance!
Do you want to finish() the activity ? All steps you have provided are correct but instead of specifically finishing it you can try this code. This finishes activity B and will start/resume activity A automatically.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
I have an action bar that looks like so:
I am using the following to see which button is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.view_all_trains:
Intent i = new Intent(ToStationActivity.this, MapActivity.class);
startActivityForResult(i, 0);
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
No matter what button you press, (the back or the pin), it goes back to the previous screen and I am not sure why. Any suggestions as to why this may happen?
Put a break statement at the end of each case of the switch!
You need to finish the case at the end using break;, else the control flow continues at the next case.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.view_all_trains:
Intent i = new Intent(ToStationActivity.this, MapActivity.class);
startActivityForResult(i, 0);
break;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
Please help me.
I use onOptionsItemSelected to get to settings layout. There I activate setDisplayHomeAsUpEnabled
public boolean onOptionsItemSelected(MenuItem settings){
setContentView(R.layout.settings);
getActionBar().setDisplayHomeAsUpEnabled(true);
return super.onOptionsItemSelected(settings);
}
How can I get back to main layout from settings layout?
In onOptionsItemSelected you need to find out whether the MenuItem that is clicked is your Home item.
I would use a switch like:
switch (settings.getItemId()) {
// If home icon is clicked return to main Activity
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
Then I would either start the MainActivity again or just call finish() to end the current one. There is a very nice article with loads of samples which you might find useful here: http://www.vogella.com/articles/AndroidActionBar/article.html
I started programming for android with eclipse a few days ago
However, I am stuck now.
I would like to add a new activity, so that i can add a new screen with new layout (an info screen about the app)
It is made so that when you press a specific menu button (help) it launches the help.xml activity and shows a new screen with some new words.
This succeeded one time, but I cant manage to do this another time.
Here is the code to direct to the activity, made from the main.java:
#Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate (R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.help:
Intent intent = new Intent(this, help.class);
startActivity(intent);
case R.id.quit:
System.exit(0);
break;
I dont have code in the help.java, and I have made some buttons in help.xml
The 'quit' button works fine but the help thing doesnt.
I am also very unsure where it links to, as the previous time I had to both make a help.java and a help.xml activity.
I already noted the help activity in the manifest.xml
Help would be appreciated, thanks in advance!
You forgot to write break after starting new Activity. That was the issue.
Replace this code:
case R.id.help:
Intent intent = new Intent(this, help.class);
startActivity(intent);
case R.id.quit:
System.exit(0);
break;
with
case R.id.help:
Intent intent = new Intent(this, help.class);
startActivity(intent);
break;
case R.id.quit:
System.exit(0);
break;
you forgot the break, and then in your help class
public class help extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
}
}