According to Google's document, getActionBar().setDisplayHomeAsUpEnabled(true) is needed to show the up button. I created a bare-bone activity using the wizard in Eclipse and specified its parent activity. I could not find any getActionBar().setDisplayHomeAsUpEnabled(true) in the automatically generated code, but the up button is present when this activity is started and it works as expected. Could anyone shed some light on this?
public class FooActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foo);
//more code...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//more code...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//more code...
return rootView;
}
}
}
When you specify a parentActivityName in your AndroidManifest, Acitivty will check for that and automatically enable the "up" affordance if it's present.
I described all possible combinations below and their outcomes:
You have both android:parentActivityName=".MyActivityand this getActionBar().setDisplayHomeAsUpEnabled(true); - back button appears and it works;
You only have this android:parentActivityName=".MyActivity - back button appears and it works, the same as above;
You only have this getActionBar().setDisplayHomeAsUpEnabled(true);, - back button appears but clicking on it doesn't go anywhere;
You have set the parameter to false in this getActionBar().setDisplayHomeAsUpEnabled(false);, even though you have this android:parentActivityName=".MyActivity in the manifest, the back button doesn't show up.
That's how this works my friend.
Related
I have an Activity that has a tabhost and a ViewPager that support 3 fragments. I want to use the ActionBar (which resides in the Activity) to use and modify menu items selectable from the ActionBar directly in each of the fragments.
What is the best way to send a message from the activity to the fragment to tell it to implement one of the actionbar items?
I have tried the following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
FragmentManager manager = getSupportFragmentManager();
List<Fragment> fs = new ArrayList<>(manager.getFragments());
int fragId = fs.get(0).getId();
PlayFragment fragment = (PlayFragment) manager.findFragmentById(fragId);
fragment.settingsMenu();
return true;
This is not working, but also is bad form to need to know the order of the fragments to get it to work. This way didn't work for me. I know I could use a broadcastSender to make it work, but that is also bad form. The REAL problem here is that since the fragments are sitting on top of a ViewPager within a TabHost, there is no way to get the id or tag reliably, so
manager.findFragmentById(fragId) and
manager.findFragmentByTag("MyFragmentName") are not useful.
When I try to attach the toolBar to an OnClickListener within the fragment, it doesn't work, and fragment has no support for
#Override public boolean onOptionsItemSelected(MenuItem item);
I'll keep looking for a solution. Thanks.
I found the answer on another post. The trick is to get the toolbar from the activity but not to use it as an actionbar directly. You need to inflate your own menu, and have access to modifying title etc ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.play_fragment, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = JamSessionApplication.getInstance();
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle(song);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_reverb) {
Toast.makeText(getActivity(), "Reverb", Toast.LENGTH_SHORT).show();
return true;
}
return true;
}
});
toolbar.inflateMenu(R.menu.menu_play);
}
I still encountered problems with ViewPager/Tabhost toobars working properly. I found an article which was much more helpful in fixing this.
https://www.numetriclabz.com/android-tabs-with-fragments-and-viewpager-tutorial/
I am trying to display custom preferences on clicking Settings options in the app. I have a listview in my MainActivity. On clicking settings, there is some INCOMPLETE VIEW displayed on top of MainActivity. And on clicking this view, the PreferenceFragment is visible with the settings layout. The listview scrolling gets disabled as a result of this view added on top of MainActivity.java. I need to display the PreferenceFragment as soon as Settings menu is clicked, instead of this incomplete view.
Please find my code below
MainActivity.java
MainActivity extends FragmentActivity{
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
}
}
PreferenceFragment.java
public class SettingsFragment extends PreferenceFragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Any help is welcome !!
You can look that post to know how to create Preference Activity :
https://stackoverflow.com/a/23545532/5652770
When I minimize, the data like score and position of objects are reset. When I lock the screen the app is restarted. Is there a way to save all objects data to make their positions and score be the same after resume? And how I can make the blockscreen button(same as wait for lockscreen) dont make the app restart. Is it possible to disable the auto lockscreen through the application?
Sorry if asking too much, I'm new here and I couldn't find what I want in other questions.
Here is my Activity class:
`public class Home extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GamePanel(this));//GamePanel is the SurfaceView
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}`
I think you should go with saveInstanceState and restoreInstanceState.
Checkout "Save Your Activity State" at http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
I have an app with a couple of activities. Until now I had a "hardcoded" hierarchy between them, so I had the android:parentActivityName set explicitly in each (but the launcher activity) of them. However now I added the Settings to the menu of more than one activity.
Is there a way to make the "back" arrow in the action bar function as the system's back button (in the bottom of the screen)? In other words: if there's a parentActivityName set, then it goes back to the parent activity, if not, then it closes the activity and "goes back" to the previous activity.
This is what I tried, but doesn't work. If I don't call actionBar.setDisplayHomeAsUpEnabled(true), then there's no "back" arrow in the action bar, but if I call it in an activity that doesn't have parentActivityName set in the Manifest, then I get an exception when I click the arrow.
abstract public class BaseActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
final ActionBar actionBar = getSupportActionBar();
if (null != actionBar) {
actionBar.setDisplayHomeAsUpEnabled(getDisplayHomeAsUpEnabled());
}
}
protected abstract int getLayoutResourceId();
abstract protected boolean getDisplayHomeAsUpEnabled();
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
final int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
}
}
Try :
if (id == android.R.id.home) {
onBackPressed();
return true;
}
The code above will call your onBackPressed, so be wary if you overrided it. Otherwise, it will be same like the "back" system button.
I am developing an Android app, and last week I updates a few packages (dont remember which ones exactly) using SDK Manager. After restarting Eclipse, whenever I create a new Activity (with used to contain only onCreate and onCreateOptionsMenu methods), it creates an Activity with those two methods, plus the folowing code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_lmain,
container, false);
return rootView;
}
}
I am new with Android so Im not sure why is that hapening and how to "undo" those changes in order to be able to create a "normal" Activity as before.
Appreciate any help, thanks.
I think you are using Android Studio and and yes, Android Studio does create one fragment and an extra method when you create your application..
It's not an issue,.. you can just delete that fragment.xml and that extra code that's with some condition and the whole additional method for the fragment.
Then it will be fine.. I don't know why exactly it happens in Android Studio.