Activity seems to be created before application object

Related searches

I have been receiving some crash reports in the Play Store which initially seemed crazy to me. Some activities (in 1 case, it's a broadcast receiver) are crashing in onCreate()/onResume() due to NullPointerException.

These activities are using static methods which in turn use the Application singleton [for context], but the object returned is null like if the application object didn't exist at all. From my understanding, the application should always have an instance.

My code is ordinary, the application sets a static field in its onCreate(), and the classes that the activities call use MyApplication.getInstance() which returns the application object.

How can MyApplication.getInstance() return null when called from activities? I have no idea how that could happen.

Of course, I have been unable to replicate this crash. This mainly happens in Android 6, but I have some reports from Android 8 and 9.

Understand the Activity Lifecycle, An Activity is an application component that provides a screen with If the activity has never existed before, the value of the Bundle object is null. it to setContentView() , you can create new View objects in your activity code� Android will create a new Application object and start the activity where the user was before to give the illusion that the application was never killed in the first place. Which means that if you expect some data to be in your application object just because your user is not supposed to be able to open activity B before activity A, you are in for a crashy surprise.

As an alternative to the other answer about dagger dependency injection I just wanted to give an example of getting context from an activity the old fashioned way. If you declare context as a member variable it will be available all over your activity and can be passed to other classes as needed:

public class ExampleActivity extends AppCompatActivity {

    private Context mContext;

    @Override protected void onCreate(Bundle savedInstanceState) {
        mContext = this;

App startup time, If all of your application's activities are still resident in memory, then the app can avoid The Displayed metric appears in the logcat output as before. from the creation of the application object to the moment reportFullyDrawn() is called. ActivityThread class in android is responsible for initiating & calling activity's life cycle method depending on related events. Creating an activity object and calling its method like you mentioned will not result your class to behave like a normal activity. – Ashlesha Sharma Mar 9 '16 at 8:04

I think I found the cause. In Android 6 auto restore not initializing app, the problem is the same and they identified the cause as being the auto backup feature that was introduced in Android 6. Basically, after a restore from the backup, the app is restarted in a weird way where the Application object is not created before the Activity. We now can reproduce the problem and disabling backup fixes the problem.

Don't Store Data in the Application Object, Android will create a new Application object and start the activity where the user was before to give the illusion that the application was never� onCreate () Called when the application is starting, before any other application objects have been created. onLowMemory () This is called when the overall system is running low on memory, and would like actively running processes to tighten their belts. onTerminate () This method is for use in emulated process environments.

app, You would usually set up listeners for the open-file and open-url events here, and start the crash reporter and auto updater. Emitted before the application starts closing its windows. A user activity can be continued only in an app that has the same developer event Event; webContents WebContents; details Object. Before anyone rants about this being an edge case - I have profiled applications that, before optimising, created 20+MB of objects in order to process ~50 rows of data. This is fine in testing, until you scale up to a hundred requests a minute and suddenly you are creating 2GB of data per minute.

Glossary -, Activities appear as a sub-category of Standard Nodes (including the Script Task, Grouping process models and their related objects in an application for the user to view before approving the task and allowing the process flow to continue. User logged into the system when a new Appian object is created, such as a� Setxl = CreateObject("Excel.Sheet") xl.Application.Workbooks.Open "newbook.xls". Many of the properties and methods that return the most common user-interface objects, such as the active cell (ActiveCellproperty), can be used without the Applicationobject qualifier. For example, instead of writing: VBCopy.

activity indicator We create the interfaces/properties for binding in the PhotoListViewModel : Each PhotoListCellViewModel object forms a canonical representation of a cell in the table view.

  • Why do you need application context? Can you use your activity's context instead? Depending on application context isn't usually something you want to do.