This is not the complete code, just the part where I have a question
I'm wondering if this:
public class MainActivity extends AppCompatActivity implements OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(this);
}
}
Is the same as this:
public class MainActivity extends AppCompatActivity implements OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new MainActivity());
}
}
Sorry if it feels like a stupid question, newbie here :)
No, absolutely not, because in the second case you are creating a new activity with no reference to the first one.
Instead in the first case you are passing the reference of the current instance of the main activity. So go for the first one :-)
When we use this or getAplicationContext() it means you are passing reference and when you use new MainActivity() means you are creating new object of same activity. In this first one is more reliable so use first one.
Related
Hi everyone i want to block the Screenshot in my app. I got the first problem solve from here.
But now the thing is I have more than 10 activity and 10 + fragment.
Is there any way to do this just by writing in the one class and giving it reference to the entire app.
Just like we make one Application class and in the AndroidMainfest.xml give that application class refrence.
You can implement a BaseActivity, and make all your activities extend this BaseActivity. In onCreate() of this activity set the flag. You need to ensure all your activities call super.onCreate() as follows:
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your flag here
...
}
}
Activity1.java
public class Activity1 extends BaseActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
}
this is my first post here. I used the search function and could not find a complete answer,so I hope this is not a redundant question.
I should note that I m really new to coding so maybe I did find an answer but did not realise it.
I have been asked in class to find two different ways to fill the argument in the code below.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(???,"Clicked!", Toast.LENGTH_LONG).show();
the first way i suppose would be toast.makeText(MainActivity.this.getActivity(),....).show();
the second one?
Use the MainActivity context.
Toast.makeText(MainActivity.this,"Clicked!", Toast.LENGTH_LONG).show();
v.getContext() and this both can be used
This makes no sense, I am given an error saying that setOnClickListener can't be applied to MainActivity. But i've made other projects where I've never encountered this problem. What's going on?
public class MainActivity extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = ((Button) findViewById(R.id.button));
button.setOnClickListener(this);
}
You have to declare MainActivity like this:
public class MainActivity extends ActionBarActivity implements View.OnClickListener
and after that you have to override the onClick method in MainActivity
#Override
public void onClick(View v) {
//do something...
}
As we can see you havn't implemented the View.OnClickListener on Main activity..
this will be used as MainActivity but the parameter that can be passed is OnClickListener
SideNote: Always try to typecast before you use, this greatly reduce the complications and sometimes takes you to mistakes you are doing.
In the following code, the class "Test" is extends Acitivity and implements OnClickListener.
but, the "this" refer to the instance of class "Test". There is no "new" to create
a new object to class "Test". So, where/when the instance of Test class created?
public class Test extends Activity implements OnClickListener {
Button playButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button) this.findViewById(R.id.Button01);
playButton.setOnClickListener(this);
}
Thanks for anyone help.
Android framework is instantiating your activities for you when you open new intents, that's why you don't need to do new ActivityClass, all you have to do is declare your activities in your AndroidManifest.
So, where/when the instance of Test class created?
The instance is created 'internally', somewhere between openIntent and onCreate methods.
So, where/when the instance of Test class created?
On calling public void onCreate(Bundle savedInstanceState) method ( in your case) since its main Activity
To make it more clear, lets change a bit your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button) this.findViewById(R.id.Button01);
OnClickListener clickListener = this; // take current instance
playButton.setOnClickListener(clickListener);
}
We register our Activity to listener by passing instance to listen on events. however we don't want that setOnClickListener will see all our methods so they ask us to provide Interface only (OnClickListener).
I want to know if Android support the possibility to make a container with some static components and just include my activities in it.
I have a sliding menu with some onClickListener events and I don't want to set these events for each activity.
If I understood you correctly, you have some functionality that is common to several Activities, and you don't want to repeat the same code in all of them. Instead, you want to do that in one place.
One way to achieve this is to create a superclass activity, place your common code in that activity, and then extend it with your other activities. For example,
public class BaseActivity extends Activity implements OnClickListener {
private Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.my_button);
button.setOnClickListener(this);
}
...
#Override
public void onClick(View view) {
int id = view.getId();
switch(id) {
case R.id.my_button:
// perform action
break;
}
}
}
Then you extend it as
public class Activity1 extends BaseActivity {
/...
}
public class Activity2 extends BaseActivity {
/...
}
public class Activity3 extends BaseActivity {
/...
}
I am not exactly sure I understand your question, can you perhaps elaborate some more? maybe even post some sample code that you are using currently.
From what I can tell you should be able to achieve what you want by making your own CustomActivity
public class CustomActivity extends Activity {
//put your slidingmenu stuff here
}
Then inside all of the other Activities where you want to use that shared piece do it like this:
public class AnotherActivity extends CustomActivity {
//...
}
with extends CustomActivity instead of the usual extends Activity
This is how i solved the problem:
First thing i did is creating my main class wich will host common code.
for example :
public abstract class main extends activity(){
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
// Your common code here
}
protected abstract int getLayoutResourceId();
}
Then all what you need is to extend this class in your activity:
public class HelloActivity extends main{
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_hello);
super.onCreate(savedInstanceState);
//make sure to put setcontentview before super.oncreate
}
#Override
protected int getLayoutResourceId() {
return R.layout.activity_hello;
}
}
All the activities needs to be registered in the Manifest. For the common things, e.g. Slide menu, you can use Fragment for the slide menu.