Can i put flurry code in onCreate() and onDestroy()? - android

I am trying to use flurry for my android app. It says that i should put flurry code in onStart() and onStop() methods. I dont have these methods in my code. I have two activities and both use onCreate() and onDestroy() methods only. Can i put flurry code in that? Will there be any problem with it?

onStart() and onStop() are methods that handle part of an activity lifecycle, so you can add them to your activities without any problem.
#Override
protected void onCreate(...) {
super.onCreate(...);
...
}
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(this, "your_key");
}
#Override
public void onStop()
{
super.onStop();
FlurryAgent.onEndSession(this);
}

onStart and onStop are existing methods on an Activity, just like onCreate. If you want to add functionality at these points of the activity lifecycle, you can override them just like you did for onCreate.
public class MyActivity extends Activity {
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(this, "FLURRYKEY");
}
#Override
public void onStop()
{
FlurryAgent.onEndSession(this);
super.onStop();
}
}
onCreate and onDestroy are not an appropriate pair of methods to use for Flurry session tracking because onDestroy is not guaranteed to be called. See the documention on onDestroy. You can end up with situations where the app gets killed by the system and Flurry will think the session is still going.
There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Related

onResume() in BaseActivity is getting called in all the activities which extends BaseActivity

In my application, bluetooth device scanning code is written in onResume() method in BaseActivity.
I extended some activities with BaseActivity so, if I switch to any activities, bluetooth scanning is being done.
Is there any way to write onResume() in BaseActivity works only once? Please help me. Thanks in advance.
Thats how inheritance works. But you can overwrite the onResume in your subclas with an empty method.
#Override
public void onResume(){
// no super class is called
}
But i think there is something wrong with your program design.
try this:
public class BaseActivity extends AppCompatActivity {
static Boolean IsResumecalled = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
if (!IsResumecalled) {
IsResumecalled= true;
//write your code here
}
}
}
Your active Bluetooth connection should not reside in the BaseActivity. It rather should bind itself on his onCreate() via bindService to the BluetoothService - so it does not matter if one Activity destroys itself and the next one is getting started, because the Service is not Acitivity-dependent.
Look here for the general android developer guide on BluetoothService
https://developer.android.com/guide/topics/connectivity/bluetooth.html
And and how to use Services namely the bounded Service
https://developer.android.com/guide/components/services.html
There are also google code examples which you can use as a guideline.
It's a bad idea to block the onResume() call since it's a important part of the lifecycle for example if you minimize and maximize the app.
Override onResume in your child activities but do not call super.onResume()
#Override
public void onResume(){
// super.onResume(); // do not call this line in your child activities
}

What is the best approach to call register/unregister eventbus on fragments?

I'm brand new using Event Bus from otto lib, So far I created a Event Bus Singleton class, which I'm using in several parts of my code. Now I'm working on a fragment view, But I still have a question, regarding:
When is the best time to register/unregister my event bus?
In a couple of posts I read that onStart() and onStop(), but without any specific reason why.
public class SomeFragment extends Fragment {
#Override
public void onStart() {
super.onStart();
EventBusSingleton.register(this);
}
#Override
public void onStop() {
super.onStop();
EventBusSingleton.unregister(this);
}
}
If I follow the same approach as in the activities doing the call onResume() and onPause() works fine as well.
public class SomeFragment extends Fragment {
#Override
public void onResume() {
super.onResume();
EventBusSingleton.register(this);
}
#Override
public void onPause() {
super.onPause();
EventBusSingleton.unregister(this);
}
}
What could be the potential risk(if exist) from each call way?
onPause()/onResume() is called when your activity does not have the focus anymore but could still be visible (think a dialog or alert on top of your activity).
onStop()/onStart() is called when your activity is not visible anymore.
Which one to use depends your use case. I believe it's not really a problem to have callbacks executed while in the paused state so I would just put the register/unregister in onStop()/onStart() but if you really want to make sure, you can put them in onPause()/onResume().
My problem was what my fragmets had two instances for bad coding, y delete de uneccesary instance and it solve the problem

Can we destroy activity without call stop() method?

In activity life cycle to execute as like.
onCreate()
onDestroy()
it means without call stop() method in activity life cycle.
how it is possible ?
Use finish(); to destroy activity.
You don't need to call stop() method. Android system automatically go thru those life cycle methods.
But apparently onDestroy() always called after onStop().
If you want to kill activity just call finish(), it will destroy your activity.
But remember again onStop() always called as system level, follows the activity life cycle if you call finish().
Note: If system kills your application or activity to utilize memory there is no guarantee to call these methods from activity life cycle.
public class ExampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty);
System.out.println("in onCreate");
finish();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("in onDestroy");
}
}
when the activity run then call onCreate() method and onDestroy()
method

Need to put #Overrides Methods for Android Life Cycle in Each Class or Just in the Main Class

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notify("onCreate");
}
#Override
protected void onPause() {
super.onPause();
notify("onPause");
}
#Override
protected void onResume() {
super.onResume();
notify("onResume");
}
#Override
protected void onStop() {
super.onStop();
notify("onStop");
}
#Override
protected void onDestroy() {
super.onDestroy();
notify("onDestroy");
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
notify("onRestoreInstanceState");
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
notify("onSaveInstanceState");
}
I'm if right, these are the methods to handle the life cycle of an activity. My question is, do you need to put these methods in each class if you want to handle your app life cycle properly, or just in the main class?
They have to be implemented once per activity. The code of one activity won't be used in anotherone unless the second is a subclass of the first.
As an additional note, only onCreate must be implemented, the others are optional.
You only need to override the methods that you want to use.
If you don't need to perform any special functions in onDestroy(), for example, you don't need to override it in any of your Activities.
A good way to identify whether or not you need it is to look at what you have in those methods. If all you are doing is calling super, then you don't need to include that method in your Activity because the default behavior will work just fine for you.
If you want your app to develop some certain behavior that can be applied to some specific part of the life cycle you have to put those methods in each class Activity. Because of the inheritance that a class have if you want some specific behavior through all the app I recommend you to use the Application class that modifies all the subsequent Activities the only problem is that not all the normal methods of an activity life cycle are available.
Here is some reference about lifecycle in Application class:
https://www.inkling.com/read/programming-android-mednieks-1st/chapter-11/life-cycle-methods-of-the
The best example of the method you always have to override is onCreate() because here is where you add all the normal start up behavior of the views and logic in your activity.
If you are not very well acquainted with android life cycle I do not recommend you to override these methods because they can lead you to strange behavior in your app, that is for my personal experience.

Do I need to call EasyTracker.getInstance().setContext in Fragment

I have an Activity, which is going to switch among various different Fragment views from time to time. In my Activity code, let's say I have
#Override
public void onStart() {
super.onStart();
... // The rest of your onStart() code.
EasyTracker.getInstance().activityStart(this); // Add this method.
}
#Override
public void onStop() {
super.onStop();
... // The rest of your onStop() code.
EasyTracker.getInstance().activityStop(this); // Add this method.
}
In every Fragment code, do I need to have?
EasyTracker.getInstance().setContext(this.getActivity());
Do I need to call EasyTracker.getInstance().setContext in Fragment?
Not necessarily. It depends on where you are using the EasyTracker in your Fragment. If your Activity's onStart() method has been called before you use the EasyTracker, then you will be fine and EasyTracker will use the Activity's Context.
However, if your Fragment uses the EasyTracker before the Activity's onStart() finishes (for example in onCreateView() or the Fragment's onStart()), then the EasyTracker will not have a Context yet and you will get an exception.

Categories

Resources