everyone. I am new in android. I am creating two activities in android ,1st activity has two image view and one button . and 2nd activity has one button. when I go to second activity and then from second activity agian . one image view of 1st activity should visibility gone. how can i do this.
here is my code
Activitymain.xml
<ImageView
android:id="@+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
}
secondactivity.xml
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
please help me
Use putextra(tag, some variable to use in mainActivity)
and Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag)
Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag)
to pass a variable (probably a boolean) to tell the onCreate() to show or not show the pic.
EDIT: Here is one way to implement it: in the first activity:
1) Check if there are extras (AKA if second activity was loaded/ there is a true)
2) If (yes) { if (first pic is loaded) { remove first pic } else if (first pick is not but second is loaded) { remove second pic } ... for all the pictures you want to do this to.
Remember you only need to pass true/false from the second activity (and then handle it in the first/one being modified)
Activitymain.xml
<ImageView
android:id="@+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getBoolean("isResult", false)){
imageView1.setVisability(View.GONE);
}
}
}
secondactivity.xml
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondIntent.putExtra("isResult", true);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.