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
Related
I have this problem. I wrote a program and I have this code in the MainActivity class:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent activity = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(activity);
output.setText(object.toString());
}
});
In AnotherActivity I modify the object and after that activity ends (with finish()) i want to "refresh" the textView of the MainActivity.
At the end of AnotherActivity the object is actually modified, but the text is not refreshed. If I click again on button, before new Activity is started (with his layout) the text is refreshed as it should, and if I close the AnotherActivity layout, the text is well refreshed. But if I don't click again that button, the text remains the old one. How can I do? Sorry for bad English or bad explanation.
You can use onResume() in the first activity to update your UI when the second activity is finished.
Either that or start the second activity with startActivityForResult which will call onActivityResult for you when the second activity is finished.
I am a beginner in java and android.
I have a categories activity with two buttons & the game activity with two layouts.I want to let the game activity detect which button is pressed in categories and set its corresponding layout by this way:
if (Case_A)
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
With what code should I replace "Case_A" and "Case_B" to detect the button press ?
When you create the Intent you can pass a value to GameActivity
Intent i = new Intent(CategoryActivity.this, GameActivity.class);
i.putExtra("layout", "layout1");
startActivity(i);
then in your GameActivity in your onCreate()
Intent intent = getIntent();
String layout = intent.getStringExtra("layout");
if (layout.equals("layout1")
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
This is one way to just pass any value you want depending on which Button was pressed. You also could get the Button text, id, or whatever you want and pass that.
I have two activities in my application. Activity1 has a list view and two buttons(say button1 and button2). Based on which button user clicks the content in the listview changes accordingly. The default loading of activity is having button1 click contents loaded in activity1.
In my Activity2, I have a button(say button3) which when clicked, has to load activity1 but with the listview loaded with button2 click results, not with the default display which shows the button1 click results.
Any help on how can I acheive this? On the onclick event of button3 in activity2 I can load activity1 but that will load with the default state showing button1 click results.
Save the state of your ListView by passing a extra string with IntentHandler when switching between the activities:
Intent intent = new Intent(getBaseContext(), yourActivity.class);
intent.putExtra("LISTVIEW_STATE", myListViewState);
startActivity(intent);
Then in your recieving activities onCreate() method (in this case yourActivity) you can get the state with:
Intent intent = getIntent();
String recievedListViewState = intent.getStringExtra("LISTVIEW_STATE");
From second activity send Intent with a flag.
In first activity always lookout for this flag and trigger your second button code accordingly. For reference you can search for "passing data between activity" here or google.
You can do this by adding Extras on your Intents. Right now, when you start Activity1 from Activity2, you probably have code that looks something like this:
Intent intent = new Intent(Activity2.this, Activity1.class);
startActivity(intent);
You can use the putExtra() method on the Intent that sets a key-value pair (called an extra) on this intent, making it look like this.
Intent intent = new Intent(Activity2.this, Activity1.class);
intent.putExtra("LIST_TO_DISPLAY", "LIST_2");
startActivity(intent);
The first argument to putExtra() (the key) is always a String, the second argument (the value) can be many different data types. Here's it's also a String.
Once Activity1 starts, you can grab the Intent by using getIntent() then pull the extra from it, all in onCreate().
protected void onCreate(Bundle b)
{
Intent intent = getIntent();
String whichList = intent.getStringExtra("LIST_TO_DISPLAY"); //which List now equals "LIST_2"
if( whichList != null && "LIST_2".equals(whichList) )
{
//Set up List 2
}
else
{
//Set up the default list
}
}
Here's the docs for the Intent Class. You can find explinations of all of the put and get Extra methods, as well as some other information that may be helpful when wanting to customize how you started Activities behave.
I dont understand so there are several things which would you think.
...
Activity1 -> Button1 -> Listview (Content A)
Activity1 -> Button2 -> ListView (Content B)
Activity1 -> Activity2 -> Button3 -> Activity1 -> ListView (Content B)
So you could
in activity1
start startActivity(intentActivity2);
In activity2
intentResult.putExtra("cmd", "button3");
onbutton click setResult(RESULT_OK,intentresult)
and finish
back in Activity1
onActivityResult
resultIntent.getExtra("cmd")
I am having some trouble with the onClick handler in a sub activity of an ActivityGroup.
I am launching a StoreActivity using:
Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);
View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();
setContentView(newVeiw);
Log.e("DEBUG", "current activity: " + getLocalActivityManager().getCurrentActivity().toString());
In the StoreActivity layout I have a button which defines an onClick method. For some reason however, it is trying to call this in the parent class that launched StoreActivity. Am I doing something wrong when launching the activity? The output of Log.e above says that StoreActivity is the current activity so I am a bit lost as to why this is happening. I can get around this by defining an onClickListener for the button in code in StoreActvity but I would like to avoid that if possible.
I think this is because you are calling setContentView from the parent activity instead of the subactivity. Why don't you just start the activity in the intent instead and set the content view in the new activity? It would be much simpler.
Try this:
Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);
startActivity(storeIntent);
and then in StoreActivity.java do:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();
setContentView(newView); //not sure if this would work, would probably be easier to put your xml layout file in here.
}
Ok I have solved this. The problem was unrelated to any of this code. I had a common base class to my activities and in that I had accidentally made the inflater a singleton. This meant that all inflated layouts belonged to the first class that created that singleton instance which happened to be the class that was incorrectly receiving the onClick event. Removing that singleton resolved this.
I am trying to make an application which have 4 tabs at the bottom of the screen.
All of them contain Activity (Intent).
And I want to navigate any of the Activity to another activity. But want to keep the TabWidget visible.
Let me know as quickly as possible if you know about it.
Shaiful
The problem of error occuring due to the replacement of activities can be solved in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
For going from list to details...
(This can be in the onClickListener)
private OnClickListener textListener = new OnClickListener() {
#Override
public void onClick(View v) {
Constants.SHOW_DETAILS = true;
Intent intent = new Intent(context, DetailsActivity.class);
replaceContentView("activity3", intent);
}
};
public void replaceContentView(String id, Intent newIntent) {
View view = ((ActivityGroup) context)
.getLocalActivityManager()
.startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
((Activity) context).setContentView(view);
}
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (MathHelper.SHOW_DETAILS) {
Log.e("back", "pressed accepted");
Constants.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent intent = new Intent(this, Tab_widget.class);
startActivity(intent);
finish();
}
}
The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
This is implemented in Tabs with multiple activities in a single tab.
However when multiple times activities are called StackOverFlow error arises. Tried very hard but unable to solve it.. Please someone tell a method to solve this problem
Also need to Replace an activity in a tab, However from child activity. How is that to be done?
At any one moment there may only be one activity. Docs about this here