Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can I make an object of Android Activity like this:
Activity activity = new Activity();
If no, then why?
And what are the problems that I may face due to this?
Can I make an object of Android Activity like Activity activity = new Activity();
Will it compile? Yes. Will it work at runtime? Probably not.
If no then why?
Because the activity will not have been set up properly. It will not appear on the screen, will not go through the lifecycle methods, will not be connected to the rest of Android, etc.
What are the problems that I may face due to this?
A never-ending series of crashes.
To show an activity, call startActivity().
Technically you can do this. But remember, you will have an instance of an Activity which is not known to Android. So you won't be able to do much with it and it won't be shown either. You might need such instance if you write tests for your application.
In your application's code you should rather never do it. Android will create an instance of Activity when you start an by calling following method.
Context.startActivity(new Intent(this, MyActivityClass.class));
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got a problem with my app when the screen turns off (because of system screen timeout) my application finishes.
I search but didn't find something helpful.
Is it a common problem or does it have a fix?
you shud not call finish() in onPause. it could be called for variety of reasons(check doc.). why you want to kill your activity when user switches app? its not recommended.
here are some posts, but there is no api available to detect app going in background.
How to detect when an Android app goes to the background and come back to the foreground
http://nathanael.hevenet.com/android-dev-detecting-when-your-app-is-in-the-background-across-activities/
I want when the user leaves this activity to finish but not the screen thnx
Take finish() out of onPause(). Put it wherever the user leaves the Activity. So, assuming you have code that starts a new Activity, put finish() after startActivity().
You also could use the flag android:noHistory in your <activity> tag of your maifest.xml so that the Activity is removed from the stack whenever it starts a new Activity. Both of these methods do the same job it just depends on how you want/need to implement it.
The reason it closes when the screen turns off is because your app calls onPause() at that time so removing the call from that method would keep it from closing when the screen turns off.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
When my app starts, the launcher activity's onCreate() method gets called normally only once. But that is immediately followed by two calls to onResume(). When I inspect the code, both instances are of the same class but only one has instanced members from the onCreate(), obviously.
Everything is working fine, though. I am just curious trying to understand why this is happening. Does anybody have any ideas? I'm using SherlockFragmentActivity, does that have anything to do with it? I'm happy to provide more information as needed. I just don't know now what else is relevant.
Thanks!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
in a part of my app, I need to start an Activity immediately, I mean it should immediately comes up and fills the screen but no matter if it takes some seconds to load the view and widgets(and it has no I/O operations). is there any specific thing in Android for such purpose?
for example base on this article:
On iOS, the system displays the launch image instantly when the user starts your application and until the app is fully ready to use.
that is about launch time, however. but I want to gain something like that.
I know I should optimize the onCreate method and avoid time-consuming operations in the UI thread but I'm searching for another specific way of staring an Activity immediately.
Sounds like the flow of code is causing you a problem. I would suggest putting nothing in the onCreate(). And the layout that has all of your elements set them all to gone. That way when the activity starts, you're given a blank black screen. Then turn all of your layout to visible via code and any of your start up task that were originally in the onCreate() can now be launched. You can use a tree observer to determine when the layout has filled the screen to fire something, like a method call or async task for example, to finish all of your loading.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Being relatively new to Android, I guess I didn't word my question properly. Here is what I am trying to do. Generally when you start a new intent, you finish the old activity by using class.this.finish() . But I want to finish a different class, not the current class. Let's say I have an activity with a button. Clicking the button would take me to a second activity. Now clicking another button takes me to a third activity and simultaneously finishes the first activity. Probably finish is the keyword here and not kill.
You could have a BroadcastReceiver registered in first activity, and send a broadcast from the third activity. Then in onReceive() method of the receiver finish() the activity.
However, re-thinking the design could be a better solution.
This is a question that doesn't have a straightforward answer. Killing activities is not a good design pattern for Android, so my first thought is "don't do this." Of course, that's not very helpful.
We may be able to help if you describe what you're trying to do. We may be able to suggest an alternative that doesn't requiring killing an Activity, or allows one Activity to finish another in a more "approved" manner.
Sometimes the answer to a problem is to suggest a different problem. Like the robot banging against the wall, the problem is not cutting through the wall, but learning how to turn and go on.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to prepare a set of guidelines and project template for my future Android projects. Im already implementing the basic MVC architecture modularity. Im trying to have add more advanced level of design to my Android projects to make my development easier and maintainable.
For example can someone suggest me a way to make Intent calls to Activity without explicitly mentioning the class while creating the Intent. Im trying to loose couple the intent calls and hopefully use AndroidManifest to assign action to an activity which then can be called globally within the application.
Any other type of suggestions are welcome.
Also please suggest any kind of coding conventions that you might be using yourself.
if you want to call an Activity without explicity set the Activity class in the Intent you could register a broadcast receiver in your activity for your custom action..
Have a look at registerReceiver function.