Program Activity Home Button to go back to previous Activity - java

After trying so hard to make Home button appear in my ActionBar it finally showed up, but when clicking it nothing happens. What method or OnClick listener should I add to make it functional.
I admit this question has been asked frequently and after going through most of them, am still not satisfied so please moderators take it easy on me, please.
Anyway your help is greatly appreciated.
Here is the code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
//Method to listen on the home button tap and go back to previous activity
}

The button in the upper left of the Action bar is the Up button. If you've already added the android:parentActivityName name to your AndroidManifest, then the system does the correct thing already - navigate you back to that parent activity as per the documentation on the onSupportNavigateUp() method, which is the method that is triggered when that button is clicked.

Related

Why my back button does nothing?

I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack

Navigating to activity after onStop() is called

When onPause is called, the Dialog is shown and it's background it's transparent so application behind is visible. So if I press Home button while Dialog is shown and then navigate back to application it shows Dialog with black background, if I dismiss Dialog application continuous normally. So how do I make background visible after navigating to application. Black background only happens when I press Home button so application is not visible. I'm guessing that something strange happens in onStop method...
This is how I call my Dialog:
pauseMenu = new PauseMenu();
pauseMenu.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pauseMenu.show();
And onCreate from Dialog class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pause_menu);
}
Your dialog is recreated automatically, so
pauseMenu.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
is not called.
So, try to call this set in your onCreate() after calling super.onCreate()

How to set imageButtons to VISIBLE at onResume() method?

I have an activity with some imageButtons in it. After I click on them i use setVisible(View.INVISIBLE); to have them gone. Now, when a user enter correct answer, a popup screen pops up with some info and OK button. I need to set all my imageButtons to be invisible when that popup window close. I tried to make some method:
private void removeImages(){
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
}
and then call it onResume:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
removeImages();
}
But it does not work, it removes all my imageButtons as soon as I start that activity. How to do that after my popup windows closes, after I press OK button on that popup?
As per the Activity Lifecycle, onResume() is called before the Actviivty is in the foreground. You have a couple different options. You can use startActviityForResult() when you click an ImageButtonand check that value in onActivityResult() to set the Views how you wish. Or you could save a value in SharedPreferences to tell the Activity which Views to set invisible/visible in onResume()

when I open an android soft, why the actionbar show for a little while?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
I hide the actionbar, why the actionBar still shows for a while.
And another question like this: I changed the actionBar's title and subtitle in code like showed. Why the actionBar's title stile show the string I put in AndroidMinefest file for a while and then changed to the title and subtitle I code in the java file. Can the actionBar show the title and subtitle at start?
As Nathan Villaescusa said, the activity is not fully loaded, so the actionbar will show, then execute actionBar.hide(), the ActionBar will show for a long time especially if the device is 3-4 years old.
As to how to prevent this situaion, Activity Theme can always be set in AndroidManifest.xml in <Activity> entry, use the #android:style/Theme.Holo.Light.NoActionBar' theme. And removeactionBar.hide()` in activity.

Refreshing a button in Android

I have set a content view in Android with:
setContentView(R.layout.activity_main);
Now after one of the buttons is clicked, the following code is executed to enable another button:
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonPause.setEnabled(true);
...
This enables the button. BUT only after a minute or so.
Do I need to refresh the button or layout? Or is that bad practice? I am wondering what causes this delay. I have read about notifyDataSetChanged(), but I do not think that is the right method.
notifyDataSetChanged() has nothing to do with Buttons, but with Adapters.
Did you try to add a buttonPause.invalidate() right after enabling it ?

Categories

Resources