How to set multiple parent activities for using android back button - android

I have an application that calls an activity several times from different activitys.
So, im trying to implement the "back button" in the action bar for this activity.
For doing this im using:
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
and:
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="view.TweetsIndividuoActivity" />
The problem now, is that i cannt set a parent activity to my android manifest, cause, i don't know who is the parent of this activity.
What is the solution ?
Thanks

It's easier than you think.
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
Method finish() will destroy your activity and show the one that started it. That's what you want if I understood you right.
Your current solution is meant for cases when you want go back to the same parent every time e.g. Gmail app does it. When you open email from notification and then press actionbar back button it will not navigate back to HOME screen but it will show you Gmail inbox.

#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
You will always go back to the activity from which you have launched the new activity.
No need to use the code below.
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="view.TweetsIndividuoActivity" />

I'm a newbie to Android too, but I solved this problem by calling the 2nd activity using "startActivityForResult(intent,1)" instead of "startActivity(intent)". I think this makes it a parent/child relationship instead of a sibling activity...?
I didn't need to use onOptionsItemSelected() or finish().

Related

Xamarin resource ID of Toolbar back/home button does not match Resource.Id.home

I have created a Toolbar for an activity in Android in Xamarin. I have enabled the back/home button with SupportActionBar.SetDisplayHomeAsUpEnabled(true);. I am trying to capture the event of pressing the back/home button with the following code, as instructed by this and many other stackoverflow posts:
public override bool OnOptionsItemSelected(IMenuItem item)
{
System.Diagnostics.Debug.WriteLine("OnOptionsItemSelected() called: " + item.ItemId);
switch (item.ItemId)
{
case Resource.Id.home:
System.Diagnostics.Debug.WriteLine("Home button pressed");
Finish();
return base.OnOptionsItemSelected(item);
default:
return base.OnOptionsItemSelected(item);
}
}
When I press the back button, OnOptionsItemSelected is called, but item.ItemId is not equal to Resource.Id.home. The former is 16908332 (tested on two different devices) but the latter is 2131492903. How can I capture the home/back button from the toolbar in Xamarin? One possible option is to hardcode the back button ID as 16908332, but I do not know if that number will stay the same permanently.
You are using the wrong resource, you want the one from the Android space:
Android.Resource.Id.Home
The right one to use is
case Android.Resource.Id.Home:
Finish();
break;

Use UP caret in ActionBar to go from Activity to Fragment - Android 4.0+

I've been researching all day/night for a solution, but it seems there are lots of options to go from an Activity to a Fragment, but none are working for me on S.O. In practice, I am in an Activity, and I want to use my app logo in the ActionBar to click it and then return to a Fragment. This Fragment is the "parent class" of my Activity, meaning there was a button in the Fragment I clicked that took me to my Activity.
But I can't get all the code snippets I've seen to work.
I have put this in my onCreate() of my Activity:
// Shows the up carat near app icon in ActionBar
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I also included this in my onOptionsItemSelected() method of my Activity:
// Handle action bar actions click
switch (item.getItemId()) {
case android.R.id.home:
android.app.FragmentManager fm= getFragmentManager();
fm.popBackStack();
return true;
default:
return super.onOptionsItemSelected(item);
}
The result is that I see a "back button" carat (as shown below), but when I click it nothing happens. I'm supposed to go back to the Fragment I came from. FYI, my Fragment class actually extends Fragment (not FragmentActivity). My Activity extends ActionBarActivity, so I am looking for an answer that will work for Android 4.0+. Also, my Fragment does not need the same instance (necessarily) when it is returned to. It only has buttons on there, so a new instance is fine, if it gets created, upon returning.
Thanks for your help!!
One small line was needed: finish(). Since the FragmentManager pops off one item in its backstack, by using fm.popBackStack();, it still needs some sort of action to go to the previous fragment. Adding finish() enabled the current Activity to end. The line in context:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case android.R.id.home:
android.app.FragmentManager fm= getFragmentManager();
fm.popBackStack();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The rest of the code I had above was correct and is needed too, to make it all work. Now, I am able to navigate to my NavigationDrawer fragment, click a button there to go to an Activity, then press the navigation UP caret to return to my Fragment at anytime. This was tested successfully on a Samsung Galaxy5 phone.
One thing that you do not read about in the Navigating Up with the App Icon section of the ActionBar Android doc is that since you are using a fragment to return to, you cannot use their <meta-data> tag instruction to specify the parent activity in the manifest file, because you are not returning to an Activity! But rather a Fragment. So a workaround had to be achieved by using FragmentManager.

Questions regarding the setting button, action bar and the title changing

I am a noob in android development and I follow the tutoral of the android website.
1. In the part of "Starting Another Activity", I just copied the code and tried to run it, but I found after the activity is changed (changed to new page), the title of action bar will change to the name of the class of that activity.
2. When it talks about the respond of the action button, the code is written as:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, in the default code:
`public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}`
It only return true which contains no method to respond(no openSettings()), but a "setting" word still pop out when I press it.
3. How do I remove the action bar and make it full screen?
Don't fully understand your question (you really didn't ask one specifically) but I think this is what you're asking
How to change the title of a new activity?
Here: How do I change the android actionbar title and icon
How does settings open up?
Android does this automatically, if the onCreateOptionsMenu(Menu menu) is called.
How do i make a full screen activity?
Here: Fullscreen Activity in Android?
In the future, be sure to look through Google and StackOverflow for you answers, most likely someone has already asked a similar question
The Name of the activity is, in your case, is declared in AndroidManifest.xml. Check the android:label attribute of your activity. You can manipulate it through java code if you want. See this SO question.
Check the menu file under res/menu/your_menu_file.xml. I think it contains something like
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
The displayed icon is NOT setting icon, but overflow icon. If the overflow icon is clicked, it lists all menu items. In your case only one (i.e Setting)
To hide the action bar, include these in your onCreate() method
//getWindow().requestWindowFeature(Window.FEATURE_NO_TITLE);
getActionBar().hide();
You can also do it via xml. Look at this SO question.

Android how to make clickable logo icon in actionbar?

I`m using SherlockActionBar for my application. In my manifest I define icon for logo and launch.
android:icon="#drawable/ic_launcher"
How to make it clickable and handle the event of it`s pressing? I want to be able to back to my dashboard by pressing the logo.
Use
actionBar.setDisplayHomeAsUpEnabled(true);
with
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can read more about the home button here http://developer.android.com/guide/topics/ui/actionbar.html#Home
It explains how to clear previous the stack of previous activities and has some more links to best practices on navigation using the home button.

Pressing the "go back" button closes the application

I'm currently moving slowly but steady forward in the making of a Android application and Im currently learning how to create a new window and switch to it. This is going all well but I have one small problem. When I pressing the "go back" button closes the application even if I have choosed to go back when just that button is pressed.
#Override
public void onBackPressed() {
finish();
return;
}
Have I missed something or what?
Thanks in advance.
EDIT
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.menuItem1:
setContentView(R.layout.about);
return true;
case R.id.menuItem2:
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT 2: About.java
package weather.right;
import weather.right.now.R;
import android.os.Bundle;
public interface About {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
You need to use Intents to "switch windows". A "window" is called an Activity in Android and you set the visual content of an Activity using the setContentView() method in the onCreate() call of the Activity.
To launch another Activity from your current Activity, simply create an Intent with a few parameters and call startActivity() with that Intent. Here's an example:
Intent i = new Intent(this, TheNextActivity.class);
startActivity(i);
Don't forget to include your second Activity in the Android manifest file. All Activities in your application must be included in that file.
Other things to note is that you don't really use System.exit() in Android. Just call finish(). It's advised to let Android manage applications and its resources rather than doing it yourself, but if you want to make sure that your application really is shut down, feel free to use System.exit() anyway. There's also no need for overriding onBackPressed() if you're only calling finish(). That's standard behaviour in Android when you hit the back button.
Also, you don't call setContentView() more than once per Activity. You start a new Activity when you need to change the visuals (or use one of the specialized Widgets to switch between layouts.
This also explains why you're experiencing your "problem". You may have changed the layout of the Activity using setContentView(), but there's still only one Activity running - when you call finish(), that Activity gets closed. If you had started a second Activity with a different layout, like you're supposed to do, Android would have closed that second Activity and would have returned you to the first.

Categories

Resources