I have coded a custom view and in this view, I have a method. When this method (launchTestActivity) is called, I would like it to launch another activity.
Currently my method looks like this:
private void launchTestActivity() {
Context ctx = getContext();
Intent intent = new Intent(ctx.getApplicationContext(), DeathScreenActivity.class);
ctx.startActivity(intent);
}
However when this method is called, it takes a time to switch to the new activity (which is just a blank screen) and I get a logcat message saying the application is skipping frames.
What do you suggest I should do?
Pretty sure it might be just your CPU or your memory that is taking a while to call this function. You are calling it correctly anyway.
The application may be doing too much work on its main thread. You should check for any heavylifting in the DeathScreenActivity.
Turns out since I was calling launchTestActivity() from within a if statement that was being tested everytime onDraw() was being called I was calling launchTestActivity() multiple times and thus launching the new activity multiple times.
Related
I have this piece of code which basically should launch an activity and act upon it, for example, set its title :
void launchAnActivityAndSetItsTitle() {
context.startActivity(intent);
activityMonitor.getCurrentActivity().getSupportActionBar().setTitle(title);
}
The activityMonitor variable is a component which holds a reference for the current foreground activity.
It is set upon the onResume() of each activity in the app.
When I run this code, the activity launches only once this method is over, which makes the second command useless, as it sets the previous activity’s title, not the new launched one. Adding a delay before the setting of the title didn’t help.
When I debugged it, I saw that the the code of the second
command is consistently being called before the onCreate() of the required new activity (regardless of the delay I set between the two). My question is why.
Thanks
EDIT - My need better explained:
I know how to pass data to a new activity. I was interested in knowing the reason for this particular timing issue - why does the activity get always launched AFTER the end of this code block/method.
According to my design, this method resides in a separate module, which is only responsible for executing a url (which is turned into a specific intent, according to a url matcher), and allowing a callback to be called after the execution of the url, i.e. after an a activity is launched.
In this example, the callback is actually a call to a method which tries to set the current activity's title (the callback method resides in the app module and is passed to the "url executor" module as an argument).
So now that I explained my need of this sort of generic approach, maybe you could give me tips on how you would do it, without falling into this "activity didn't really launched yet" problem.
Thanks.
If you would like to modify something in the activity being started by the intent I would recommend passing an Extra along with your Intent and getting that extra in the new activity OnCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxxxxxxx);
Intent i = getIntent();
setTitle(i.getStringExtra("title");
To put an extra inside an Intent use intent.putExtra("title", "newTitle");
The life cycle of an activity is documented in many places but I couldn't find the thing I need. This is my activity, it have a constructor and the onCreate method. In my project I have also a logging in this methods and every time when I go from portrait to landscape I see that both methods are executed. Why my the constructor is called ? isn't the activity in the stack and the instance of my activity is in the memory so when the configuration change is happen, then only the oncreate and on retainistancestate should happen (of course the onResume). Why the constructor is called every time, who is calling ? Is it every time when something get changed from the configuration both methods are guaranteed to be called (one after another, in this same sequence).
public TestActivity()
{
super(R.menu.main_menu, tag);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
I was playing with my sample app but I want to know more details, can someone clarify me the scenario when the constructor is included ?, I founded a a lot of documentation about life-cycle but none explains the details when the constructor is included
Edit1:
I read in some places that there is stack in witch the activities are putted in so the next time they go up and running faster, but what when the configuration get changed ? Is it must to to call the constructor and the oncreate methods ?
On rotation your activity will be restarted complete. You can prevent that with android:configChanges="keyboardHidden| orientation" in your manifest.
As #rekire answered, the activity is restarted on screen rotation. Here restart means that the framework creates another instance of the activity, that's why the constructor of your activity class is called and then the onCreate(). The new activity instance replaces the old one which will be finally recycled by GC, if its reference isn't held by others.
If you want to avoid activity restart on screen rotation, please read this question.
I've drawn an UML diagram to describe the Android activity life cycle.
Hence there are no reason having constructor to invoke activity unless you have constructor with params(onCreate invoke it for us anyway...). However basically it seems like a java thing onCreate probably calling activties's default constructor which is
public ActivityName(){} // This might get call because onCreate somewhere under the hood invoking Activity :)
Try same thing with constructor with param like
public ActivityName(String s){}// This wouldn't be call unless you explicitly call it.
Hope this will help,
I would wait for more expert answer though :)
Edit : So when you rotate your phone which calls onCreate as it will get created again and onCreate probably calls default constructor to invoke instance of your activity :)... I forgot to mention this earlier.
Im clearing up the bitmaps i load in one activity before i more into the other activity.
eg:
pic1 = null;
System.gc();
nextActivityIntent = new Intent(ThisActivity,NextActivity.class);
ThisActivity.startActivityForResult(nextActivityIntent,123);
But the problem is system calls onDraw some times after i call "pic1=null". When it happens the application crashes with pointing a NullPoint Exception.
Cab any one suggest me how to stop calling onDraw() after setting the "pic1=null". Can i use synchornized to make this happen.
You can try view.setWillNotDraw(true); (link here), but I'm not sure it will prevent it.
On a side note, if your first activity will finish, there is no need to set the bitmap to null nor to call System.gc();. The bitmaps will be recovered anyway after the activity is destroyed.
If for some reason you still want to do it, you should do that inside the onDestroy() callback to avoid any drawing issues. Check the Activity lifecycle for more details.
I have two apps that both extend activities and work perfect on their own, but when I call intent of one to view data in the other the app crashes. I have taken the second and made it a separate java file in my app. Also, I have updated the manifest to include this new activity. Where do I call the intent? If all I want from the second activity is for it to get data and return data should I use startactivityforresult instead? The main thing I want to do is get sensor data from the orientation sensor and show it on my display while still running the original activity.
This is how I call the second activity:
Intent intent = new Intent(this, LeanAngle.class);{
startActivity(intent);
Maybe the problem is where I am calling it. Is there a specific place to call it? Also, it has the same view as the main activity does, r.layout.main.
You are calling it correctly. I assume your placement is wrong. It will only work if it is the last thing you call in onCreate(). If that is indeed where it is located.
There is something I don't quite understand right now.
My main activity class creates a Service, which creates a new thread that waits for a TCP connection. Once one comes in, it will start a new activity:
Intent dialogIntent = new Intent(getBaseContext(), VoIPCall.class);
dialogIntent.putExtra("inetAddress", clientSocket.getInetAddress());
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
After that, the onCreate() method of that class gets run. It will create 2 threads: one records and send data, the other one receive and plays data. Those threads have a forever while loop.
For some reason, I notice that the onCreate() of that last class gets called again, which makes my program crash. I do not understand why it is called again as only the 2 threads are running, there is no user interaction. The documentation says: "Called when the activity is first created.". The activity is already running and I am not trying to create it.
Could someone please explain me this behavior?
Android will recreate your activity after certain "device configuration changes". One such example is orientation. You can read more here...
http://developer.android.com/guide/topics/resources/runtime-changes.html
Perhaps something in your threads is doing something which is considered a configuration change?
If that's the case you might find it useful to extend the Application class instead and do your initialization there. See this post...
Activity restart on rotation Android
HTH
I was experiencing an Activity called twice on some Samsung devices. I solved it adding android:launchMode = "singleInstance" on the Activity tag on Manifest. I hope this can help.
Similar to this question where orientation change is causing the device to reconfigure:
Activity restart on rotation Android
My preferred answer:
https://stackoverflow.com/a/11811999/3739540
Instead of trying to stop the onCreate() from being fired altogether, maybe try checking the Bundle savedInstanceState being passed into the event to see if it is null or not.
For instance, if I have some logic that should be run when the Activity is truly created, not on every orientation change, I only run that logic in the onCreate() only if the savedInstanceState is null.
Otherwise, I still want the layout to redraw properly for the orientation.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_list);
if(savedInstanceState == null){
setupCloudMessaging();
}
}
This happened to me once when I used "Don't save actions" in the app section of the developer options. Be sure you have turned this off.
I have observed this issue when you are trying start an activity with values in the intent.
Below is an example where Activity_A calls Activity_B and passes values in the intent to be collected in Activity_B:
Intent intent = new Intent(this, activityB.class);
intent.putExtra("val1", someValue1);
intent.putExtra("val2", someValue2);
intent.putExtra("val3", someValue3);
this.StartActivity(intent);
In this case, you can set the android:launchMode="singleInstance" or android:launchModel="singleTop" in your AndroidManifest.xml and Activity_B will only launch once. Hope this helps.