I had a fragment where it contains its call back methods. I want to know the method tracing for instance which method is called when. I tried by calling Debug.startmethodTracing("calc") in onCreateView() and Debug.stopMethodTracing() in onDestroy() method. The "calc" file is created with no data in it.I mean nothing is written into that file. Can any one let me know am I doing any wrong in calling Debug.stopMethodTracing().
Thanks in Advance
Related
in my app, there are 5 classes and 1 activity. This activity has, as you all know, an OnDestroy() method. In this method I need to remove a test provider which is set up in another class called "mockingclass".
In "mockingclass" I have a method similar to this:
public void mocker()
{
xxx
location.RemoveTestProvider(location.GpsProvider);
xxx
}
While xxx stands for many other functions in this method, when the app is being destroyed I need to call ONLY for that ONE function in within this whole method.
Is there any way to do that at all? If not, what would be your suggestions?
THANKS!
Obviously you can't choose a single line from method to be executed. You need to extract it separate method which will be called separately. Maybe you should look at some injection? Let this call be injected as an delegate for example. In that way you could manipulate what should be called depending on situation.
I have a button that I want to change its value often, so my Activity has a private variable :
private Button p1_button = (Button)findViewById(R.id.firstbut);
This simple line makes my app crash. If I put inside the onCreate it works and I can interact with the button (change text etc).
EDIT : I think I found the reason. I should initialize AFTER setcontentview ?
EDIT: Thank you for the constructive answers. I have now a different problem I removed the initialization and I did it on onCreate and it works (But I keeped the p1_button declaration as a private field). But when I tried to modify the button in a different method of my activity (just changing the text), it crashes again. So the return value of findViewById is "local" to the method where it is called and I should setcontentview in every method that access UI elements ?
Do not call findViewById() until after you call setContentView(). Otherwise, the widget will not exist.
More generally, do not call inherited methods on Activity until after super.onCreate(), unless specifically advised to do so.
It depends where you are calling this line.
http://i.stack.imgur.com/6EQaU.png
The onCreate() method contains a call to setContentView() and before this is called, Android has no idea what to do with your button as it hasn't been inflated yet!
Therefore as a really easy rule of thumb, always make sure setContentView (or if you're dealing with fragments onCreateView()) have been called and completed. Only then will findViewById() work.
If you would like further guidance, please post some code in which the crash occurs.
edit: I tried to add the image properly but don't have enough rep.
To understand this you need to know the Activity lifecycle. You are trying to look a view which has not yet been created by Android.
As per the android lifecycle explained here "http://developer.android.com/training/basics/activity-lifecycle/starting.html". In onCreate() method the activity is created and you can access different views of the activity. If you will try to look for view before onCreate() the app will crash as it does not know whether that view exists or not.
I know onCreate , onListItemClick and onPageSelected etc. are callback methods in android.
Yet some are not so obvious to tell they are callback methods,
for example, getItem method in class FragmentStatePagerAdapter.
(Isn't it? Or getItem is not a callback method?)
How can we know a method is a callback method in android?
Is there any clue to let people know they are callback methods?
Or is there a list of all callback methods in android?
The documentation for each class at the Android developers site will list all of the methods available to you as a developer.
Take the Activity class for example. We can generally sort the methods into two large buckets:
Methods that are final such as getApplication()
Methods that are not final, such as onCreate()
Methods that fall into the first bucket you can immediately rule out from what you refer to as "callback" methods. Since they are final, you cannot override their behavior.
Methods that fall into the second bucket might be a "callback." I wouldn't get hung up on trying to label them as such though. Each and every method in the Activity class may or may not get called by some other piece of code at some point in time. However, most of those methods exist because they will get called at some point in time.
Even some of the methods that you might not think of as a "callback" method can be overridden and can be called by the framework (or your own code). For example, you can override the finish() method of an Activity. Most applications will only ever call finsh() directly without customizing the behavior, but you do have that option.
Focus on what you need to do, find the appropriate method to either call or override, then read the documentation on that method. Whether or not the method is a "callback" is usually irrelevant.
I've created a Master-Detail application from the given Android templates. I need to create a method to delete an item on longclick, but all the tutorials I've found on the subject involve
getListView().setOnItemLongClickListener...
// and so on
My problem is, I don't know where the ListView is! When I run the program with no modifications, just the base template Android creates, it runs fine. When I add "getListView" to the onCreate method, the program crashes.
There's obviously a ListView, because I see a list, but I don't know where in the default template it's buried. It's not listed in any xml or java files, so I don't know how to access it.
This may be a stupid question, but I've been at this for hours, and I would really appreciate any advice.
If you've created from Master-Detail template, you have a Fragment generated that extends ListFragment. Trying to call getListView() from onCreate() method in the Fragment will result in the following exception:
java.lang.IllegalStateException: Content view not yet created
You can't call this method before the view of the Fragment has been created (as the exception states). You can use it in the onViewCreated() method (or any method called after onViewCreated() was called). Hope this helps.
getListView is a method from ListActivity. If you are not extending that, you won't see it
Situation
My activity waits on an Async operation and after it hears back from async operation, it needs to pass information to 2 fragments inside it.
Requirement
1. Both fragments need their onCreateView calls to be done for them to have their layouts loaded,
2. They need for themselves to be attached to their activity so that getActivity() works.
I wrote a setData() method in both the fragments and am looking for the "correct" place in the activity's lifecycle to invoke them.
onCreate() of the activity does not work, onStart() of the activity does not work and onStart() of the fragment does not work.
Nothing works, what am I missing here?
The official documentation for the Fragment lifecycle explains this clearly - please refer to it and then ask follow-up questions if something is unclear.
This Image will be helpful to understand both life cycles together.
As many people complaints and it is somewhat valid argument that this life cycle is too complicated, in Google I/O 2018,They have suggested to use Architecture component Framework. Please check this Docs
when you are at Activity2---->backpress--->Fragment2(Activity1)---means Activity1 again attach from fragment2 so on OnAactivityCreated() method Activity1 is completely loaded ....so at that we can call setData() method of your Activity1...
onAttachFragment()-activity is called before onCreate()-activity and after onAttach()-fragment
Call onDestroy on onStop of your fragment. This should call onCreate when the fragment is launched.
Let me know if works as an ideal solution for your problem.