In my main screen I have an ImageButton which is disabled in the xml file for the main activity. After some other activities are executed I return to the main screen (using finish()) and I want to find this ImageButton enabled. How can I do this?
Here's what I tried. I wanted to use the name of the ImageButton (btn_grand) as in the following, but it does not work because btn_grand is null:
public void Exit(View view){
//enable image buttons on the main screen
finish();
DataTrak dt = new DataTrak();
this.setContentView(R.layout.activity_main);
dt.btn_grand.setEnabled(true);
dt.btn_grand.setClickable(true);
}
DataTrak is the name of the main activity. The layout file for DataTrak is activity_main.xml. Exit is executed when I click on a button and it belongs to another activity (the current activity).
Could you please help?
You never want to instantiate an Activity
this way
DataTrak dt = new DataTrak();
they are not like a normal Java class. Only do it with startActivity()
. What you could do is pass an extra to the Activity
and check it there. Something like
Intent i = new Intent(v.getContext(), DataTrak.class);
i.putExtra("someKey", true); // where someKey is any key you want to use
startActivity(i);
Then in onCreate()
or onResume()
of your DataTrak
Activity
(depending on if you have finished it) check that value
Intent i = getIntent();
boolean enable = i.getBooleanExtra("someKey", false); // false for default value
btn_grand.setEnabled(enable); // if you passed the value it will enable else is disabled