Android - Class extending RelativeLayout (onResume) - android

I have this class: public class HeaderView extends RelativeLayout implements OnClickListener, ProgressBarListener
In every Activity, this HeaderView is shown as I put it in the XML layout.
Now, the thing is that I want to be able to implement onResume in my HeaderView. When I launch an Activity, onCreate is called and the HeaderView draw the things I need at the top, but if I press back, my Activity will call onResume and my HeaderView is not going to be recreated and the old values will still be there. How can I implement onResume in my HeaderView or something similiar in order to recreate the HeaderView!?
Thanks in advance!

you cant call onResume in the HeaderView class as its is the callback method of the Activity class and the classes extending the Activity class.So in the onResume of your Activity class, call a method of the HeaderView class something like refreshView(), and then your view can be refreshed.Editted section: You can make a BaseScreen class extending Activity and implement this thing in its onResume.Now extend this class in all your activities instead of the Activity class.This will make to run refreshView() every time onResume is called an you no longer need to define it in every activity class of your code.

Related

Change TextView in other activity

I would like to change the TextView in an Fragment from my MainActivity. As the MainActivity contains a NavigationDrawer I have to change the TextView without such things like startActivityForResult.
I've also tried the LayoutInflater method, but it changes the whole layout, instead of just a small TextView.
Edit:
MainActivity:
new FRAGMENT_Main().setBalance(credits);
Fragment_Main:
public void setBalance(String credits)
{
AmountCredits.setText(credits);
}
Thank you very much!
If i get your question
If the MainActivity has the Instance of that fragment.
Just make a function in Fragment to update the textview.
and call it from MainActivity using the fragment's instance.

Use onClick Listener for a button located in one of tab fragment, in Main Activity?

Can I use onClick Listener for a button located in one of tab fragment, in Main Activity?
I think you want to perform some action in your MAIN ACTIVITY when a button is clicked in your fragment. If I am right, then just create a interface callback from your fragment to the activity and use the over-ridden method to perform whatever action you require.
Have a look at this
http://developer.android.com/training/basics/fragments/communicating.html
or this
onAttach callback from fragment to activity
Hope this helps.
Yes, you can think of a Fragment as a stand alone user interface that contains it's own animations and logic. So an onClickListener for a Button in a Fragment is no different than in an Activity.
Though the method using Interface is sufficient, this is the simple way to do it.
You can pass context("getActivity()") of MainActivity while calling
the fragment.
Create a method in MainActivity to change the fragment.
call the MainActivity method to set the Fragment.
((MainActivity) context).setFragment(fragment);
you are done.

How to call mypageradapter.notifyDataSetChanged() from another activity?

My app has the next structure, I have the Mainactivity where I have my viewPager. Inside this viewpager I add new fragment Activities. These Fragment Activities have a TextView with their names, I have created a OnlongClickListener in the textview so that when you OnlongClick in the textview the fragment Activity gets removed from the list in the viewpager I have in the main Activity.
I've tried to use in the fragment Activity the next code :
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
but the result is an empty space in the position where the deleted fragment was.
I think the solution would be calling mypageradapter.notifyDataSetChanged() in the fragment Activity but the thing is that the viewPager and the pagerAdapter are created in the mainActivity and I want to notify the changes in the fragment activity.
How can I call mypageradapter.notifyDataSetChanged() from the fragment activity?
In this moment I have a solution for this problem but I don't think this solution is the best one, when I delete the fragment I create an intent for the mainActivity and then I StartActivity(intent). In the mainActivity onStart method I call mypageradapter.notifyDataSetChanged() and then the empty spaces dissapear and the fragment list is sorted correctly. This solution is really slow and rough.
I would appreciate a better solution for my problem.
P.D: For help I have MyPagerAdapter class inside the mainActivity.

Access an Activity from a Fragment plugged into another Activity

I have been using this example as a base for my project.
I have changed the project to use an ArrayAdapter for the titles-fragment's ListItems and changed the DetailsFragment's View to display a custom layout, which as a Button that is supposed to add an entry into a database.
Instead of generating widgets like in the example, I just inflate a custom XML into the FrameLayout besides the TitlesFragment in the 'layout-land' version of the layout.
My problem stems from the fact that DetailsFragment is plugged into the MainActivity when in Landscape mode but gets it's own DetailsActivty if it is in Portrait mode.
The Button I have in my custom layout for the details-fragment calls a function in its onClick() that is called AddNewItem.
So when the Button was clicked in Landscape mode, it crashed, because there was no AddNewItem in the MainActivity. I solved this by using a BroadcastReceiver, so when the button is clicked, a method named AddNewItem in MainActivity.java instead broadcasts a custom event and i have a BroadcastReceiver that calls the AddNewItem in DetailsFragment.java.
It looks like this:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
DetailsActivity m = (DetailsActivity) DetailsActivity.getActivityInstance(); // see below for what getActivityInstance does
LayoutInflater mInf = LayoutInflater.from(context);
View myView = mInf.inflate(R.layout.customlayout, null);
((DetailsActivity) m).AddNewItem(myView);
}
}
But this gives me:
08-11 13:37:50.687: E/AndroidRuntime(6766): java.lang.RuntimeException: Unable to start receiver in.falkeninc.umt_v0_9_8_1.MyReceiver: java.lang.NullPointerException
I am not sure what is happening. Because I am also using a static variable in DetailsActivity.java to be able to reach it inside the BroadcastReceiver. The code looks like this:
public class DetailsActivity extends SherlockFragmentActivity {
...
public static SherlockFragmentActivity activityInstance;
...
activityInstance = this; // in the onCreate
...
public static SherlockFragmentActivity getActivityInstance(){
return activityInstance;
}
}
My problem stems from the fact that DetailsFragment is plugged into
the MainActivity when in Landscape mode but gets it's own
DetailsActivty if it is in Portrait mode.
This shouldn't be a problem. Although you have two activities you have the same DetailsFragment class in both of them. If a Button from that fragment is doing something then you should keep it at that fragment's level(that would be an ideal fragment, one that is self contained, a fragment that doesn't know or care where is put). If you do require the activity for the work in that method, you should add extra details.
Also, if the behavior is common to both activities you could make a base activity holding that method and let the two current activities inherit from that, so it will be available to the fragment no matter what.
public static SherlockFragmentActivity activityInstance;
Don't keep static references to activities, you risk leaking them.
Also, don't access activities from outside their own context. When an activity is not the one interacting with the user(it's onPause() has been called) that activity could as well be completely destroyed and trying to access it in another activity could bring you lots of problems.

Calling activity method from extended view class in Android

This is a problem I didn't forsee when designing the structure of my applicaton.
Basically i have 3 classes.
ProfileActivity //The main activity for the application.
ProfileView //Which extends Linear Layout so that I can tinker with my own view. Note that this is just part of the whole activity and not the main layout
ProfileData //This is just a class for storing data.
The activity contains multiple ProfileViews which each contain one profileData.
Heres where I'm stuck. My profile View has an on click method assigned which needs to call a populate method in the activity. Unfortunately ````
//I'm inside the activity
public void populateProfileDataForm(ProfileData pd)
{
//edit some text fields and other widgets from here
}
Is there any way to call the activity method from the profileView class?
If not, then the error is in my design and can anyone point me towards a better solution for binding data, view and activitys?
When you create a view, you always need a context, and always pass a activity to it.
So try use below code:
If(m_context instanceof ProfileActivity)
{
ProfileActivity activity = (ProfileActivity)m_context;
// Then call the method in the activity.
}
Or write an interface onProfileViewClickListener such as view.onclickListener. then the ProfileActivity implements it and set it to the ProfileView.
What about if you assign an OnClickListener or OnTouchListener in your ProfileActivity Class and in this Listener you cann call the populate Method. In the OnTouchListener you can get the Position of the TouchEvent so you can assign it to a specific Profile.

Categories

Resources