Search code examples
javaandroidbuttonlisteners

Android - How to make buttons listen more than one time


I have an app with a home screen and a bunch of buttons on it, and therefore listeners for each. After the user clicks on one of the buttons, a new layout is brought up and that layout has a back button with a listener.

The problem is that whenever the user presses the back button, the home screen layout is brought back up but none of the listeners work for the buttons anymore.

Here is some sample code:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); // return to home screen

    // sets up a listener for when the GCF main screen button is clicked.
    GCFButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            setContentView(R.layout.gcf); // change to the gcf layout
            Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
            back.setOnClickListener(new View.OnClickListener() // put a listener on back button
            {
                public void onClick(View v) 
                {
                    setContentView(R.layout.main); // return to home screen
                }
            });

            Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
            GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
            {
                public void onClick(View v)
                {
                    // do stuff
                }
            });
        }
    });
}

Solution

  • I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.

    The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener, when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.

    To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners from the home screen again.

    I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...), as well as when the back button is clicked:

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        listeners(); // set up all the listeners for the buttons    
    }
    
    public void listeners()
    {
        setContentView(R.layout.main); // return to home screen
        // sets up a listener for when the GCF main screen button is clicked.
        GCFButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                setContentView(R.layout.gcf); // change to the gcf layout
                Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
                back.setOnClickListener(new View.OnClickListener() // put a listener on back button
                {
                    public void onClick(View v) 
                    {
                        listeners(); // recursively call the listeners again to 'start over'
                    }
                });
    
                Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
                GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
                {
                    public void onClick(View v)
                    {
                        // do stuff
                }
            });
        }
    });
    

    }

    I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.