问题
This question extends this thread How to hide ActionBar and NavigationBar after a certain delay time?
This works for my application. But it only runs once. How can I make it to keep executing until a button is pressed (back button)
Btw this is a photoViewer app (part of) like Gmail app on android when you view a photo.
Update1: I actually have a touchListener to show() and hide() the actionBar again. So what I want is when I'm in imageView layout, the actionBar will disappear in 3000ms, then I tap to show(), then after 3000ms it should disappear again. Again, just like Gmail android app when you load a picture from email
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); // overlay mode
setContentView(R.layout.fullscreen_image);
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// time delay to hide actionBar
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
// DO DELAYED STUFF
getActionBar().hide();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}, 3000); // e.g. 3000 milliseconds
....
Button back = (Button)findViewById(R.id.btnBack);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
回答1:
I think you should move the creation of the timer-thread to your onClick(), so that every time it gets click, a new timer is started.
回答2:
placing your timer in OnCreate ensures it will run once and only once. If you want it to run everytime place the user rotates the screen, pauses or whatever, put it in onResume. But, as popovitsj suggests, putting on the onClick is likely what you want
来源:https://stackoverflow.com/questions/19845182/android-loop-in-oncreate-method-possible-or-not