How to implement parent methods to activity and FragmentActivity? - android

I am working on an android application,
and I have number of activities that extend from a custom activity.
Now I created a new FragmentActivity and I need the same functions that I have implemented in my custom activity.
How can I do that?
Edit
This is an simple example
public class BaseActivity extends Activity
{
protected void someFunction()
{
Log.i("TEST","This is a test");
}
}
public class MainActivity extends BaseActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
someFunction();
}
}
this is my FragmentActivity:
public class MyFragmentActivity extends FragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// here I want to use someFunction()..
}
}

In Java you can't extend more than one class.
So I guess you should extend BaseActivity from FragmentActivity instead of Activity

Related

how to implement preferenceactivity in activity extending from application

How can I do to implement a preferenceactivity in a class that does not extend from activity to application, I share a bit of my code:
This is my MainClass AppBeacon:
public class appBeacon extends Application implements BootstrapNotifier {
some code...
}
This is the preferenceactivity class:
public class PreferenceActivity extends android.preference.PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preference);
...some code
}
});
}
}
How to implement my preference activity in Appbeacon??

Calling a method from another class is causing app to crash

I decided to try and make my code more object oriented and avoid repetitive code in another class.
Source code for Activities :
public class EasyMode extends MainActivity {
GameActivityPVP game = new GameActivityPVP();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_layout_pvp);
game.initializeButtons();
}
}
public class GameActivityPVP extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_layout_pvp);
initializeButtons();
}
public void initializeButtons() {
button[0] = (Button) findViewById(R.id.button1);
}
}
The second the program gets to the line where I try to call a method using game.methodName(); the program crashes. No compiling errors or anything.
I am new to programming in general so please take it easy on me and I tried to simplify my code as much as possible.
Android Monitor/logcat :
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
and
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
You can use another class's method by creating object of parent class.
See below example;
Here you want to use method from 'GameActivityPVP' class. So you need to create one object in this class only.
public class GameActivityPVP extends MainActivity {
public static GameActivityPVP mGameActivity;
public GameActivityPVP getInstance(){
return mGameActivity; // assign value in onCreate() method.
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_layout_pvp);
mGameActivity = this; // Do not forget this, otherwise you'll get Exception here.
initializeButtons();
}
public void initializeButtons() {
button[0] = (Button) findViewById(R.id.button1);
}
}
Now use this Object in another class 'EasyMode' like this;
if(GameActivityPVP.getInstance()!=null){
GameActivityPVP.getInstance().initializeButtons();
}
Try This:
Make one Class Utils:
In Utils:
public class Utils{
private Activity context;
Button button;
public Utils(Activity context) {
this.context=context;
}
public void inititializeButton(Activity context){
button[0]= (Button) context.findViewById(R.id.button_flasher);
}
}
And in your Class use:
public class EasyMode extends MainActivity {
Utils utils;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_layout_pvp);
utils=new Utils(this);
utils.initializeButtons();
}
}
As already stated, you shouldn't use nested activities, they are not supposed to interact like this. If you want two activities to interact you have to do it through an intent. Regarding the duplicated code, you have few solution presented but my personal opinion is that the OOP rules are not followed. If I had to write that logic, I would create a BaseActivity to hold the common logic of the other two activities and use inheritance to extend them.
public class BaseActivity extends Activity {
protected List<Button> buttons = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_layout_pvp);
initializeButtons();
}
protected void initializeButtons() {
buttons.add((Button) findViewById(R.id.button1));
}
}
public class EasyMode extends BaseActivity {
// Add here logic that is used only in EasyMode activity
}
public class GameActivityPVP extends BaseActivity {
// Add here logic that is used only in GameActivityPVP activity
}
Note that in this way you don't have to override onCreate again to initialise the buttons and so on. Also, I saw that you used the same layout for both activities, but if you want to use different layouts you can do it as usual and then call initializeButtons.

How can I get the same drawerLayout on multiple types of activities without using fragments?

I want to have the same drawerlayout for multiple types of activities.
This works, but how would I be able to use this with a different type of Activity? A FragmentActivity for example.
My base activity:
public class BaseDrawerActivity extends Activity {
protected DrawerLayout _drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
....stuff with drawerlayout
}
}
And then I extend the class in my stat activity:
public class StartActivity extends BaseDrawerActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater startInflater = getLayoutInflater();
View contentView = startInflater.inflate(R.layout.activity_start, null, false);
_drawerLayout.addView(contentView);
}
}
Extending from different classes not allowed in Java. So in that case you'll need to extend it from FragmentActivity.

Android Main Activity Object Access

Im new to Android and got a problem. I wanted to seperate the OnClickListener from the MainActivity class but I don't know how to access the Objects in the Main Activity class except of using static. Would be happy about a solution.
public class MainActivity extends ActionBarActivity {
public Button btn;
public TextView tv;
public MyListener listener;
public MainActivity() {
this.listener = new MyListener();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btn = (Button) findViewById(R.id.btn1);
this.tv = (TextView) findViewById(R.id.textView1);
this.btn.setOnClickListener(listener);
}
}
public class MyListener implements OnClickListener {
MainActivity activity;
#Override
public void onClick(View v) {
activity.tv.setText("Hi");
}
}
Doesnt work.
I would appreciate any help.
I don't see why you would want them separated like that.
If you don't want to cram the onCreate method of your activity, just implement the listener in your class like so:
public class MainActivity extends ActionBarActivity implements OnClickListener {
...
}
Then reference the listener with this:
this.btn.setOnClickListener(this);
MainActivity is a Activity class. You should not have constructors for the same. You should not instantiate a activity class. You only declare activities in manifest file
You can have this.listener = new MyListener(); in onCreate itself
Make MyListener an inner class of MainActivity

How to set the titlebar in every activity programmatically?

Is it possible to set the titlebar in every activity programmatiaclly from one particular activity?
I can set the titlebar of a particular activity programmatically from within that activity, but subsequent activities have the string set from the manifest thereafter.
To set a title I use the following from within the activity. I would like every activity set like this in the application.
setTitle(carerName + " is now logged in");
you can write base activity and call a method of super .
public class DerivedActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class DerivedActivity2 extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class BaseActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(carerName + " is now logged in");
}
}

Categories

Resources