I have just started Android development. I have a very simple project. I have a button and a textview. When I click the button, the text of the textView changes and button is disabled. This part is working fine. However I want the button to maintain its state (disabled state) if I press home on phone (Escape on keyboard) and go back to my app. But in my case, Button gets enabled every time I press home and get back to my app. Here is the code
package com.mypack.textmanipulation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
private Button btnChangeText;
private boolean check;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnChangeText = (Button)findViewById(R.id.btnChangeText);
check = true;
setButtonOnClickInterface();
}
@Override
protected void onResume()
{
super.onResume();
checkButtonIsEnabled();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void setButtonOnClickInterface()
{
btnChangeText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
TextView txtView = (TextView)findViewById(R.id.textView1);
txtView.setText("Text Changed");
btnChangeText.setEnabled(false);
check = false;
}
});
}
private void checkButtonIsEnabled()
{
if (check)
{
btnChangeText.setEnabled(true);
}
else
{
btnChangeText.setEnabled(false);
}
}
}
Suggestions are welcome. Thanks
Well just found the solution. I was exiting the app instead of pausing it. Actually i was presing the ESC button on the keyboard to go back to HOME screen which is actually equivalent to the BACK button in Android and in my case, i guess, the app was exiting, so every time i tapped the app icon again, onCreate event is been called instead of onResume.
BTW HOME button on Keyboard acts as Android HOME button.