为啥显示我的按钮,然后快速隐藏 onResume
Posted
技术标签:
【中文标题】为啥显示我的按钮,然后快速隐藏 onResume【英文标题】:Why my button is shown and than quickly hidden onResume为什么显示我的按钮,然后快速隐藏 onResume 【发布时间】:2015-10-01 10:01:52 【问题描述】:这可能只是一个愚蠢的错误,但我无法修复它。我有一个 Google+ 登录按钮,当用户未登录时会显示。我还有一个退出按钮,当用户登录时它会消失。除了我回到活动时,一切正常( onResume) 我可以看到红色的 Google+ 按钮大约一秒钟,然后它被隐藏并出现退出按钮。如何删除这一秒,在此期间我仍然可以看到 Google+ 按钮?
这是我的布局:
XML 代码:
<ImageView
android:layout_
android:layout_
android:id="@+id/startGameView"
android:src="@drawable/play"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_
android:layout_
android:layout_above="@+id/startGameView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46dp">
<!-- show achievements -->
<Button
android:id="@+id/show_achievements"
android:layout_
android:layout_
android:text="Achievements"/>
<!-- show leaderboards -->
<Button
android:id="@+id/show_leaderboard"
android:layout_
android:layout_
android:text="Leaderboard"/>
</LinearLayout>
活动中的代码:
public class StartActivity extends BaseGameActivity implements View.OnClickListener
private ImageView mPlay;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
mPlay = (ImageView)findViewById(R.id.startGameView);
mPlay.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//play animations
YoYo.with(Techniques.Pulse)
.duration(200)
.playOn(findViewById(R.id.startGameView));
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
);
findViewById(R.id.sign_in_button).setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
findViewById(R.id.show_achievements).setOnClickListener(this);
findViewById(R.id.show_leaderboard).setOnClickListener(this);
//mSignOutButton = (Button)findViewById(R.id.sign_out_button);
@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_start, 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);
@Override
public void onSignInFailed()
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_button).setVisibility(View.GONE);
@Override
public void onSignInSucceeded()
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
@Override
public void onClick(View view)
if (view.getId() == R.id.sign_in_button)
beginUserInitiatedSignIn();
else if (view.getId() == R.id.sign_out_button)
signOut();
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_button).setVisibility(View.GONE);
else if (view.getId() == R.id.show_achievements)
Toast.makeText(StartActivity.this,"achivements",Toast.LENGTH_SHORT).show();
startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), 1);
else if(view.getId() == R.id.show_leaderboard)
Toast.makeText(StartActivity.this,"leaderboard",Toast.LENGTH_SHORT).show();
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(
getApiClient(), getString(R.string.number_of_solved_math_problems_leaderboard)), 2);
BaseActivity 代码:
public abstract class BaseGameActivity extends FragmentActivity implements
GameHelper.GameHelperListener
// The game helper object. This class is mainly a wrapper around this object.
protected GameHelper mHelper;
// We expose these constants here because we don't want users of this class
// to have to know about GameHelper at all.
public static final int CLIENT_GAMES = GameHelper.CLIENT_GAMES;
public static final int CLIENT_APPSTATE = GameHelper.CLIENT_APPSTATE;
public static final int CLIENT_PLUS = GameHelper.CLIENT_PLUS;
public static final int CLIENT_ALL = GameHelper.CLIENT_ALL;
// Requested clients. By default, that's just the games client.
protected int mRequestedClients = CLIENT_GAMES;
private final static String TAG = "BaseGameActivity";
protected boolean mDebugLog = false;
/** Constructs a BaseGameActivity with default client (GamesClient). */
protected BaseGameActivity()
super();
/**
* Constructs a BaseGameActivity with the requested clients.
* @param requestedClients The requested clients (a combination of CLIENT_GAMES,
* CLIENT_PLUS and CLIENT_APPSTATE).
*/
protected BaseGameActivity(int requestedClients)
super();
setRequestedClients(requestedClients);
/**
* Sets the requested clients. The preferred way to set the requested clients is
* via the constructor, but this method is available if for some reason your code
* cannot do this in the constructor. This must be called before onCreate or getGameHelper()
* in order to have any effect. If called after onCreate()/getGameHelper(), this method
* is a no-op.
*
* @param requestedClients A combination of the flags CLIENT_GAMES, CLIENT_PLUS
* and CLIENT_APPSTATE, or CLIENT_ALL to request all available clients.
*/
protected void setRequestedClients(int requestedClients)
mRequestedClients = requestedClients;
public GameHelper getGameHelper()
if (mHelper == null)
mHelper = new GameHelper(this, mRequestedClients);
mHelper.enableDebugLog(mDebugLog);
return mHelper;
@Override
protected void onCreate(Bundle b)
super.onCreate(b);
if (mHelper == null)
getGameHelper();
mHelper.setup(this);
@Override
protected void onStart()
super.onStart();
mHelper.onStart(this);
@Override
protected void onStop()
super.onStop();
mHelper.onStop();
@Override
protected void onActivityResult(int request, int response, Intent data)
super.onActivityResult(request, response, data);
mHelper.onActivityResult(request, response, data);
protected GoogleApiClient getApiClient()
return mHelper.getApiClient();
protected boolean isSignedIn()
return mHelper.isSignedIn();
protected void beginUserInitiatedSignIn()
mHelper.beginUserInitiatedSignIn();
protected void signOut()
mHelper.signOut();
protected void showAlert(String message)
mHelper.makeSimpleDialog(message).show();
protected void showAlert(String title, String message)
mHelper.makeSimpleDialog(title, message).show();
protected void enableDebugLog(boolean enabled)
mDebugLog = true;
if (mHelper != null)
mHelper.enableDebugLog(enabled);
@Deprecated
protected void enableDebugLog(boolean enabled, String tag)
Log.w(TAG, "BaseGameActivity.enabledDebugLog(bool,String) is " +
"deprecated. Use enableDebugLog(boolean)");
enableDebugLog(enabled);
protected String getInvitationId()
return mHelper.getInvitationId();
protected void reconnectClient()
mHelper.reconnectClient();
protected boolean hasSignInError()
return mHelper.hasSignInError();
protected GameHelper.SignInFailureReason getSignInError()
return mHelper.getSignInError();
【问题讨论】:
您能添加您的 BaseGameActivity 代码吗? 我确实为 BaseActivity 添加了代码。如果您正在寻找覆盖 onResume 方法,则此活动不会执行此操作。我已经检查过了:/ 在布局中默认隐藏按钮。在 onResume 中,您只需切换可见性。体验会以这种方式得到改善。 谢谢,我试试这个。 @sushantkunal 谢谢,效果很好!我添加了这个:protected void onResume() super.onResume(); if(findViewById(R.id.sign_in_button).getVisibility() == View.VISIBLE) findViewById(R.id.sign_out_button).setVisibility(View.INVISIBLE); else //findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE); findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);如果您想添加您的建议作为答案,我可以接受!谢谢! 【参考方案1】:如果您最近克隆(或刷新)示例,您会注意到不再使用 GameHelper 和 BaseGameActivity。有一个有关此更改的信息视频:Game On! - The death of BaseGameActivity。如果您只是实现接口以获取状态回调:GoogleApiClient.ConnectionCallbacks
和 GoogleApiClient.OnConnectionFailedListener
,您的问题应该会消失。
关于如何使用这些接口的更多具体细节在https://developers.google.com/games/services/training/signin
【讨论】:
感谢您的回答。我不知道这两个类已被弃用。我会尝试这种方法:)【参考方案2】:正如我在GameHelper class 中看到的,它在onStop()
上与googleApiClient 断开连接并在onStart()
上连接。这是导致按钮闪烁的原因。
如果您不想更改 GameHelper
的实现,请进行一些 UI 改进以使其不那么烦人。
【讨论】:
感谢您的建议。但是,我已经应用了@sushant 的建议,并且效果很好。以上是关于为啥显示我的按钮,然后快速隐藏 onResume的主要内容,如果未能解决你的问题,请参考以下文章