我在firstActivity(MainActivity)
中有两个图像视图,当我点击signIn图像然后它移动到SignUp Activity
...
在这里,当我点击signUp图像然后再次来到MainActivity
..在这里我必须使firstName
图像不可见..
public class MainActivity extends Activity {
ImageView firstName,signIn ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstName =(ImageView)findViewById(R.id.imageView1);
signIn =(ImageView)findViewById(R.id.imageView2);
signIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),SignUp.class);
startActivity(intent);
}
});
}
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
String mm ="5";
Intent i= getIntent(); String s = i.getStringExtra("PrevAct");
if (mm ==s) {
firstName.setVisibility(View.GONE);
}
}
public class SignUp extends Activity {
ImageView signUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
signUp =(ImageView)findViewById(R.id.imageView3);
signUp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("PrevAct","5");
startActivity(intent);
}
});
}}
答案 0 :(得分:0)
当您尝试再次启动MainActivity Activity时,请确保您重复使用相同的实例,而不是新实例,以提高应用程序的性能。
Intent intent = new Intent(getApplicationContext(),MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); intent.putExtra(" PrevAct""注册&#34); startActivity(意图);
使用变量检查屏幕是否来自SignUpActivity。
意图i = getIntent(); String s = i.getExtra(" PrevAct"," NO");
根据字符串值,您可以决定显示/隐藏。
请注意,View.Invisible
仅隐藏屏幕上的视图。但它仍然被加载并占据屏幕上的空间。这是一个糟糕的UI实现。
因此请改用View.GONE
。
您的源代码修改如下
public class MainActivity extends Activity {
ImageView firstName,signIn ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstName =(ImageView)findViewById(R.id.imageView1);
signIn =(ImageView)findViewById(R.id.imageView2);
signIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),SignUp.class);
startActivity(intent);
}
});
}
protected void onStart() {
super.onStart();
firstName = (ImageView)findViewById(R.id.textView1);
{
Intent i= getIntent();
if(i!=null){
String s = i.getExtra("PrevAct","NO");
if(s.equalsIgnoreCase("SignUP"))
firstName.setVisibility(View.GONE);
}
else
firstName.setVisibility(View.Visible);
}
}
public class SignUp extends Activity {
ImageView signUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
signUp =(ImageView)findViewById(R.id.imageView3);
signUp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("PrevAct","SignUP");
startActivity(intent);
}
});
}}
答案 1 :(得分:0)