chandu chandu - 22 days ago 3
Android Question

I am getting doubts in android button clicking

Hi in my code i have two activity in first activity there is two buttons one is visible another is invisible if i click first button second button have to visible and i have to go next activity on that activity there is one button if click that button it have to come first activity now second button have to display first button have to invisible please help me.

mainactivity.java



package com.example.admin.mypratic;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button frst,scnd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frst=(Button)findViewById(R.id.firstbtn);
scnd=(Button)findViewById(R.id.scndbtn);
frst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
frst.setVisibility(View.GONE);
scnd.setVisibility(View.VISIBLE);
startActivity(intent);
finish();
}
});
}
}


mainactivity2.java



package com.example.admin.mypratic;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
Button b3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b3=(Button)findViewById(R.id.third);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
}
}

Answer

If you want to see button 2 instead of button 1 when you are coming back from second activity, you don't start first activity rather you finish you current activity (MainActivity2) and android will pull first activity from the Intent stack. [Also, you don't finish first activity after the call from second activity to hold it's status/state]. startActivity restart your activity, so the onCreate is fired and the default layout is loaded.

so the newly modified button codes: in mainActivity1

frst.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            Intent intent=new Intent(this,Main2Activity.class);
            frst.setVisibility(View.GONE);
            scnd.setVisibility(View.VISIBLE);
            startActivity(intent);
        }
    });

In second activity

b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

needless to say, you need to have an onClickListener for your button 2, so that button 1 is visible for on click.

Cheers mate!