Subactivity onClick Firing in Parent Class - android

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.

Related

How to start a fragment from a RecyclerView Adapter which is inside another fragment?

I have a tab view with two fragments. Those two fragments contain a recycler view with cards.
Each card in both fragments had a button.
Clicking on fragment 1's button should open the fragment 2 as a separate page and vice-versa.
I am struggling to find a method to implement this without making every too complex and tightly coupled.
This is fragment one with its own Adapter.
And this is fragment two:
Clicking on that SELECT DONOR button in Donees page should open donor fragment in a new page where the user will be able to assign a donor for the selected donee.
So I have two needs here
1) To start a fragment from a fragment
2) To Keep track from which Donee the new donor page was opened so that I can assign a donor for that specific donee.
I hope this is understandable.
so far I have tried LocalBroadcast and FragmentManager but its hard to keep track of what I'm doing with the code.
Can you guys suggest a better technique to achieve this ?
the easiest solution would probably be, starting a new activity, passing something like an ID, name or something to the intent on an Button click.
Context.startActivity(new Intent(Context, YourAssigneeActivity.class)
.putExtra("ID",id));
So I assume that you do not switch to the other tab when you click a button on one tab. Therefore the fragment should fill the whole screen.
With this assumption in mind you most likely have to switch the Activity. This can be dones easily with an Intent:
Intent intent = new Intent(getActivity(), ActivityB.class)
intent.putExtra("KEY", <your required data to transfer>);
getActivity().startActivityForResult(intent);
Note that when you use putExtra() don't forget that you need to implement Parcelable in those objects (explained here)
To get to know which item was clicked you can use the following pattern (pseudocode - I personally think it's really clean):
FragmentA implements YourAdapter.callback {
onItemClicked(<YourObject> item) {
<starting new activity as described above>
}
}
class YourAdapter extends RecyclerView.Adapter {
Callback mCallback;
YourAdapter(Context context, otherStuff) {
mCallback = (Callback) context;
}
interface Callback {
onItemClicked(<YourObject> item)
}
YourViewHolder implements View.OnClickListener {
onClick(View v) {
mCallback.onItemClicked(<YourObject> item)
}
}
}
Once you are in your Activity, you can set the Fragment in onCreate() of your Activity. In the Activity retrieve the data with getIntent() in the onCreate before creating the Fragment. Then you can put your data in the Fragment with setArguments(<Bundle>). In the Fragment in the onCreateview() retrieve it with getArguments().
I know this is kind of conmplicated. A better solution would be to just switch to an Activity and forget about the Fragment. This would remove one layer of complexity.
If you directly go from Fragment to Fragment you can ignore the Activity part but the rest should stay the same.
Hope this was the answer you were looking for!
Edit: Note that mCallback = (Callback) context is null if the Activity is not implementing Callback

Android layout doesn't "refresh" until I start a new activity

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.

Enabling an ImageButton from another activity

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

Android: I have created a new activity, I called it but the window doesn't have the GUI

(I am running this on a Nexus 7) I created a new activity and I have called on it properly. The new window appears correctly, except none of the GUI elements that I declared int eh XML file appear in this new window. How do I fix that?
In my main activity:
final Intent mapIntent = new Intent(this, MapActivity.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
Button b = (Button)findViewById(R.id.MapButton);//Finds the button w/id "MapButton"
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(mapIntent);
}
});
And the called activity (MapActivity) is empty except for the super.onCreate(SavedInstanceState) line in onCreate().
Let me guess: you didn't call setContentView(R.id.xml_file_layout) in your Activity onCreate() - just after the call to super.onCreate(savedInstanceState)!
I think you need to set the layout in MapActivity.
setContentView(R.layout.your_layout);
If you are calling super() then always place it properly, just 1st line of the method. Hope this will help you, if you have correctly get what I'm saying.

Several activities using same listener

I got 4 activites that all include a xml-footer which contains 4 buttons (one for each activity).
I would now like to setup onclicklisteners to these buttons (it's a self made menu in the footer).
The question is, how do I use listeners so that I can reuse code?
I have two ideas:
Create a class that implements onclicklistener and in every activity i would get the buttons and then create a new instance of the listener class and do button.setOnClickListener(onClickListener)
The problem is that in the listener class, how would i check which button called the event?
And how would I create an intent to start an activity, usually i would do:
Intent intent = new Intent(FromActivity.this, ToAcitivty.class)
But i don't have the reference to FromActivity.
Create a base class that extends from activity and then the 4 activies will extend from the base class. I would then like to setup the listeners in the base class. The problem here is that i can't get the references to the buttons by doing
Button button1 = (Button)findViewById(R.id.menu_button1);
button1 will be null. I haven't even called setEventView because this should be done in the activity not in the base class.
Any ideas?
Thank you
Same code is here:
public class MyClass extends Activity implements View.OnClickListener{
btnA=(Button)findViewById(R.id.btnA);
btnA.setOnClickListener(this);
btnB=(Button)findViewById(R.id.btnB);
btnB.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
Button clickedButton = (Button) v;
switch (clickedButton.getId())
{
case R.id.btnA:
Intent regIntent = new Intent(Home.this,Registration.class);
startActivityIfNeeded(regIntent, 1);
break;
case R.id.btnB:
//Some code
break;
}
}
(edited as the original first line is broken on code format.

Categories

Resources