Call method from other class - android

Ok I am a complete newb when it comes to java classes. I have a public method that dynamically displays some Linearlayouts with some stuff in them. For instance this method (public void methodA)is in ClassA.java, then I want to call methodA from inside ClassB.java. Both of the classes extend Activity and the methodA is being called in the OnCreate method.
ClassA.java
public class ClassA extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodA();
}
public void methodA() {
//Do Stuff
/* This uses:
* Package Manager
* Buttons using(this)
* Linear Layouts using(this)
* TextViews using(this)
* findViewById()
* startActivity
*/
}
}
ClassB.java
public class ClassB extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodA(); //How do I do this
}
}

It is clear to me that the method is specific to the first activity, which means that it shouldn't be called in the second one. You can either reimplement the method in the second activity or, if these activities are similar (don't do this if they aren't!), you have two options:
Inherit the second activity from the first one.
Merge these two activities into one and use different intents to launch them and act accordingly.
The second method is easier to maintain, so I would prefer it over the first one in simpler cases.

Craete instance of class A in class B and then you can invoke MethodA from class B
public class ClassB extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//MethodA(); //How do I do this
ClassA a=new ClassA ();
a.MethodA();
}
}

Related

Can you use a constructor in Child Activity to change Parent Activity String variable?

I have a Parent Activity class that all my Child Activity classes extend from.
This parent activity has a field called 'activity_Id' that is a String and defaults to be null.
In the onCreate of the Parent Activity, I need to check if that field has a value, which it would only get from the Child Activity if it does I do some other logic in the onCreate of the parent activity.
Note, that each activity has a unique 'activity_Id' that it is used to alter the other logic that is being done.
Here is the ParentActivity:
public class ParentActivity extends Activity{
protected String activity_Id = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(activity_Id != null)
//DO OTHER LOGIC UNIQUE TO THIS STRING ID
}
}
Here is the ChildActivity:
public class ChildActivity extends ParentActivity{
public ChildActivity(){
super();
activity_Id = "Foo123";
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
This activity_Id String is predetermined but does not match the Activities name so I can not do
this.getClass().getName()
in my ParentActivity class.
This seems like the cleanliness way BUT there seems to be some taboo around using constructors in activities so I just want to make sure I do not break anything. Or if someone can think of a cleaner way to do it?
This can be done with abstract class because constructor don't construct an activity directly.
Try it like this
public abstract class ParentActivity extends Activity{
protected abstract String activityId();
.....
}
Then in child activity override activityId() with required value. And now you can compare activityId() in ParentActivity onCreate(...) method.

How to prevent the Screentshot in the entire android app without repeating the same code

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);
...
}
}

Call method in abstract activity after onCreate of derived activity

I have an abstract Activity that serves as a basis for other activities:
Since I am settings the content view in the derived Activities and I have common UI elements in all activities, I would like to execute code in the derived Activities after the content view has been set.
How can can I do this in the abstract class without putting the method call in every derived Activity?
abstract public class BaseActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void methodToBeCalledAfterOnCreateOfDerivedActivity(){
//method that does work on common UI elements, so setContentView() needs to have been called
}
}
public class myActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//how can I call this method automatically at the end of this onCreate without explicitly putting the method here:
methodToBeCalledAfterOnCreateOfDerivedActivity();
}
}
The sequence of calls when an Activity is created the first time or the view needs to be recreated is:
onCreate()
onCreateView()
onViewCreated()
onViewStateRestored()
onStart()
onResume()
So, if you only need your method to be called when the view is created, you could do it in onViewCreated(). If you need it to run whenever your activity is restarted, you would do it in onStart().
onStart() is invoked after onCreate(). You can probably put your post-onCreate() functionality there. Use a flag set in the abstract class onCreate() to distinguish between onStart() invocations following onCreate() and onStop()-onRestart().

Use a container for Android activities

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.

Call Public void

I have a public void in one class and I want to call it in another class when it creates but nothing seems to be working. here is the code of my first activity
public class activityone extends Activity {
public void actionC() {
//actions
}
Does anyone know how to call it in my second class?
In general, you need to have an instance of your activityone class in order to call an instance method.
To create an instance, you generally use a constructor like:
activityone a = new activityone();
a.actionC();
I'm not sure this is what you want though, because Activitys are generally created by the Android system itself and you should handle the onCreate method instead.
Here is what you can do:
public class activityone extends Activity {
/*public void actionC() {*/ //Instead on normal method, write your actions in onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//actions
}
and in your second activity, do this:
Intent intent = new Intent(getApplicationContext(),activityone.class);
startActivity(intent);
Hope it helps !!!

Categories

Resources