为什么显示我的按钮而不是快速隐藏 onResume
Why my button is shown and than quickly hidden onResume
这可能只是一个愚蠢的错误,但我无法修复它。我有一个 Google+ 登录按钮,当用户未登录时显示。我还有一个注销按钮,当用户登录时该按钮消失 in.Everything 除了当我返回到 activity (onResume),我可以看到红色的 Google+ 按钮大约一秒钟,然后它被隐藏并且出现注销按钮。如何删除我仍然可以看到 Google+ 按钮的这一秒?
这是我的布局:
XML代码:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/startGameView"
android:src="@drawable/play"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startGameView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46dp">
<!-- show achievements -->
<Button
android:id="@+id/show_achievements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Achievements"/>
<!-- show leaderboards -->
<Button
android:id="@+id/show_leaderboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leaderboard"/>
</LinearLayout>
activity中的代码:
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();
}
正如我在 GameHelper class 中看到的那样,它在 onStop()
上与 googleApiClient 断开连接并在 onStart()
上连接。这是导致按钮闪烁的原因。
如果您不想更改 GameHelper
实现,请进行一些 UI 改进以使其不那么烦人。
如果您最近克隆(或刷新)示例,您会注意到不再使用 GameHelper 和 BaseGameActivity。有一个关于此更改的信息视频:Game On! - The death of BaseGameActivity。如果您只是实现接口以获取状态的回调:GoogleApiClient.ConnectionCallbacks
和 GoogleApiClient.OnConnectionFailedListener
您的问题应该消失。
关于如何使用这些接口的更多具体细节在 https://developers.google.com/games/services/training/signin
这可能只是一个愚蠢的错误,但我无法修复它。我有一个 Google+ 登录按钮,当用户未登录时显示。我还有一个注销按钮,当用户登录时该按钮消失 in.Everything 除了当我返回到 activity (onResume),我可以看到红色的 Google+ 按钮大约一秒钟,然后它被隐藏并且出现注销按钮。如何删除我仍然可以看到 Google+ 按钮的这一秒?
这是我的布局:
XML代码:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/startGameView"
android:src="@drawable/play"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startGameView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46dp">
<!-- show achievements -->
<Button
android:id="@+id/show_achievements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Achievements"/>
<!-- show leaderboards -->
<Button
android:id="@+id/show_leaderboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leaderboard"/>
</LinearLayout>
activity中的代码:
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();
}
正如我在 GameHelper class 中看到的那样,它在 onStop()
上与 googleApiClient 断开连接并在 onStart()
上连接。这是导致按钮闪烁的原因。
如果您不想更改 GameHelper
实现,请进行一些 UI 改进以使其不那么烦人。
如果您最近克隆(或刷新)示例,您会注意到不再使用 GameHelper 和 BaseGameActivity。有一个关于此更改的信息视频:Game On! - The death of BaseGameActivity。如果您只是实现接口以获取状态的回调:GoogleApiClient.ConnectionCallbacks
和 GoogleApiClient.OnConnectionFailedListener
您的问题应该消失。
关于如何使用这些接口的更多具体细节在 https://developers.google.com/games/services/training/signin