What is the default activity if you don't override onCreate? - android

I am new to both Android and Java (as well as OOP), so forgive me if this is super noobish. In going through the Android tutorial on a first app, the following code is written:
#Override
protected void onCreate(Bundle savedInstanceState){...}
I have read that #Override is overriding the default onCreate call and that, if you don't override, you can't specify which activity to use. That's all well and good, but in the interest of understanding, what exactly is the default activity if activity_main is not in the case that you don't override onCreate?

I think what you are asking is what does the base class onCreate method do, and what happens if you don't override it. You can look at the source code for Activity here, or the AppCompatActivity here to look at what is in the base class. Notice that a typical implementation in an app looks like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// other custom setup code for your activity
}
That first call to super.onCreate still calls the base class onCreate method (the one you are overriding), so you're not replacing what's in the base class, but adding your customizations specific to the Activity you are creating. That call to super does a number of Activity setup things that you always have to do. It doesn't associate the activity with any layout file (which is done by the setContentView call), so it wouldn't display anything from your xml files without you adding the setContentView call. There is no default xml layout file it would use.

I believe you mean the default Activity layout?
If you don't set one in onCreate with setContentView I think you simply won't have one.

Related

I am using android studio 8.0

I am using android studio 8.0. and I made a simple activity to go to another activity here are these errors are shown
You don't need to call onCreate() explicitely. Once you declare an instance of a class, if it has the onCreate() class, it will be automatically called at the appropiate time.
If you override that class, always be sure to declare the #Override statement above it, this will make sure you're overriding the correct function because if you don't, and you don't specify the correct parameters that the former method has, it will not be called and you'll think it does.
Also, it's a good idea to call super.onCreate(savedInstance) as the first line of your overriden method.
---- EDIT ----
As said above, onCreate() is called on the object creation, i.e., when you declare: alertFunction alertClass = new alertFunction();.
If this is something you want to call several times, put the content in a public function inside your class and call it from onCreate() and from the outside when needed. For instance:
public void myFunction() {
// Put here the current code of onCreate()
...
}
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
myFunction();
}
And then if you need to call it again from outside your class, do the following:
alertClass.myFunction();
Source: ERROR: The method onCreate(Bundle) in the type alertFunction is not applicable for the arguments ()
By the way, there is no Android Studio 8.0. Check out this link: https://developer.android.com/studio/releases/index.html
Hope this helps! Good luck!

use android:onClick with methods from another activity?

I have menu xml which included in my activities, im using onClick attribute instead of binding on every startActivity.
My question is, how can I define on the xml onClick attr to call methods which is located on my mainActivity for example?
I thought about something like android:onClick="mainActivity.doSomething" but its doesnt work.
Using onclick in xml is a bad idea. The fact is it only tries to find the method that the current class is in which is using it. One way to do this IF you really want to do it this way is:
startActivity:
public void callOtherMethod(){
mainActivity.doStuff();
}
mainActivity:
public static void doStuff(){
//dosomething.
}
startActivity.xml:
android:onClick="callOtherMethod"
Your doStuff method must also be static, unless you can get a instance of the target method Activity.
I thought about something like
android:onClick="mainActivity.doSomething" but its doesnt work.
It's normal, Android looks for the onClick method declared in the xml layout only in the current activity where that layout file is used(and it wouldn't make sense anyway to look in other activities as those activities could be well destroyed when you call that method).
My question is, how can I define on the xml onClick attr to call
methods which is located on my mainActivity for example?
You should explain what you're trying to do. Accessing methods of an Activity from another Activity should be avoided, it's not the proper way of doing things in Android.

adding a method to android application

I am fairly new in android and Java and this question might seem a little bit a naive question ,
I am trying to write a method called getDispalay() , this will be a public with no retuen.
I will print a message saying I am in the getDisplay method .
In java it is very simple , It will be called in main method , But I do not know how i can use main method in activity class ? I mean how can I write a method and call it as I need it ?
Thanks
Go through this article
As you said in java we call the function in main method .
In android if java class extends activity you can call the method in Any of the life cycle event of activity , It depends on your need . For example if you want to call method when you enter into activity follow this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)//To set up ui for activity
getDispalay();
}
public void getDispalay(){
//your actions
}
What you really need to do is understand the activity lifecycle:
Once you have read through that then you will know how your Android app runs and what states and stages it transitions through.
You should read the documentation about Activity Lifecycle.
The onCreate() method would be comparable to main().

Do I need to implement the onCreate(), onResume, etc methods in every activity I write?

I have started to explore the android world just some days ago, and I am doing it via the book by Mario Zechner, "Beginning Android Games".
I might have a ton of questions about the platform and the few things I have seen so far but I know it's going to get better. All I want to ask at the moment is about activities: I saw the activity life cycle. I know that activities are something like screens. The thing I don't know is that whether I have to specify the onCreate(), onResume() etc. methods in every activity I code.
As far as I know onCreate() is compulsory and the other methods depends on how you use the activity
The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). So onCreate(Bundle) should have be there in activity.
Use of onResume() depends upon your application requirement.
for more details go to http://developer.android.com/reference/android/app/Activity.html
Welcome to the world of Android.
In general, it is good practice to flesh out all the methods such as onPause(), onResume() but when you create an android program, generally you only need to flesh out the onCreate() method for activities.
Besides the onCreate, and pardon if my terminology is incorrect, the other methods follow a "default" behavior if you do not override them. So if you need the application to do something specific when it is paused, that would be a good time to add your version of onPause(), otherwise you can leave it left out.
It is not mandatory that you have to specify all those methods or any of them. It depends on what type of implementation you want
Example
I have created my Activity (A) as it extends Activity I donot override any of methods like onCreate() but I have created some variables and created some of methods.
Let us assume I created second activity there I want to shoe some view I have used onCreate() method also If I want variables and methods which I have defined in activity A I can get those variables and methods If I write class B extends A
So it is not mandatory to use all those methods from activity. If you donot write your own implementation then the default implementation will come to play.
Short answer will be NO
You don't need to specify in code of each Activity onCreate and so on. Anyway in parent Activity there will be onCreate
But long answer says: good practice is not rely on implicit/invisible code, but to have code under your control (even if it's dummy). I used to code all onCreate/onDestroy, etc in this way:
public static final boolean DEBUG=true;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(DEBUG)
Log.d(TAG, "Creating "+this.toString());
}
You should write onCreate() method by overriding it from the base class Activity to set the view. View should be generated here itself using setContentView() method in onCreate() method.
Regarding onResume(), onPause() and other methods, it is not mandatory to write these but are useful when you need to achieve specific functionality.
Also, being a beginner please have a look at the table 1 in this document: http://developer.android.com/guide/topics/fundamentals/activities.html

How to always run some piece of code in android regardless of which activity or service run first in android?

I would like to run a piece of code every time any activity or service is started. In this piece of code, I might do things such as set the default uncaught exception handler and manage log levels.
The problem is that I have an activity which starts with the user clicking the application icon. I have another which starts if a certain intent is broadcasted, possibly from another app and possibly called before the user click the launch icon. Same goes for services.
I need to guarantee that a certain piece of code will be run while keeping the code clean; that is to say, without having to manually add that snippet of code to every activity and service class that I have.
Could you not extend the basic Activity class for Android like this:
public class MyClass extends Activity {
public void onCreate(Bundle bundle) {
//Add custom code here
}
}
Then have all of your actual "Activity"'s in your application extend the custom class?
public class MyInterfaceClass extends MyClass {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//Other code here
}
}
That way all your custom code will be called when the Activity starts up.
For an application called Wibble...
public class Wibble extends Application {
protected static void DoSomething()
{
// Do your common code here
}
}
Then extend Activity...
public class WibbleActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Wibble.DoSomething();
}
}
Then derive all activity classes from WibbleActivity...
public class Whatever extends WibbleActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// WibbleActivity calls Wibble.DoSomething()
// so the 'Whatever' class doesn't have to.
}
}
Any Activity derived from WibbleActivity will run Wibble.DoSomething() implicitly.
If you want Services in the mix I can't help - I'm an Android 'rookie' and haven't got on to Services yet but I suspect extending your own app-level Service class might work in the same way.
You could extend Application and do it in its onCreate() method.
You have two choices
A) You can manually add the code - it might be only two lines importing and instantiating something from a source file you copy in unmodified - to every separate component that you write. It will only be in your projects, not in other people's unless they do it too.
B) You can, after no small difficulty, learn to make your own custom version of android that automatically does this each time it starts up a suitable component, and install this customized version on developer phones or hacked consumer phones.
"started" is ambiguous - what are you referring to? onCreate? onResume?
In any case, your best bet is to have a separate class with a static method that does the code you are talking about, and you call that in every single onCreate (or onResume, whichever you need) of each one of your activities.
That, or you create your own Activity subclass and derive all your activites from it, and override onCreate (or onResume). All your onCreate/onResume implementations are required to call the superclass' implementation, so you're guaranteed to have your code caled.

Categories

Resources