How do I create common code for parts of Android activities? - android

In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)
What are the possible ways to do this?

The common way is:
Create a super class called, for instance, CommonActivity which extends Activity
Put the boilerplate code inside that class
Then make your activities extend CommonActivity instead of Activity:
Here a simple example:
public class CommonActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
// code that is repeated
}
protected void moreRepeatitiveCode(){
}
}
And your current activities:
public class AnActivity extends CommonActivity{
public void onCreate(Bundle b){
super.onCreate(b);
// specific code
}
}

Hmm.. Common code doesn't always need to be in Activity class but just regular class. Than we could call those methods according to our needs referring to the common code class.
Am I right with this example?
Of course in case we need it like Activity, above proposal would work perfectly if we take care of Activity lifecycle and we don't forget to add it to manifest file.
In general Activities should just create UI, handle events occurrences and delegate business logic and/or other actions to the other components in our App.
Cheers

Related

share argument between all fragments

I am making a bottom navigation app and want to share data between all fragments, for example clicking a button to share a name for all action bars. With safeArgs I can only send data to one fragment.
And I want to ask, is any solutions for global arguments(global variables) like redux in react native or provider in flutter?
Thank you
You can use shareviewmodel, it's best practice
Link refer: viewmodel
EDITED
Just create a public variable as a class variable in your MainActivity
public class MainActivity extends AppCompatActivity{
public int myVar; // outside of all methods
...
Then you have access it everywhere in your Fragments inside that MainActivity, like this:
MainActivity mainActivity = ((MainActivity) getActivity());
if (mainActivity != null)
mainActivity.myVar = 15;

Android, should all code be written in an Activity

I am more familiar with iOS development than Android and I am wondering if all code should be written in an Activity rather than having a "model" class.
I have a couple screens each with a few checkboxes and I want them all to behave the same on click, I am trying to figure out how I would do this without writing repeating code in each activity. Thanks!
No you should not. If you are familiar with java, think of an activity as a extension of main with OO added.
In your particular example you can create a class with a method like:
<MethodName>(View <checkboxClickedName>){ //your code here }. and then add this to the checkbox in the XML android:onClick="<MethodName>", you may need the full package path (e.g. com.example.app.)
Note: if some of the commands/objects you need are only available within an activity you should create this in an calss that extends Activity or preferably within the running activity.
You could have a base class that extends activity that contains the methods that you want executed on click (either implemented or abstract). Use this new base class instead of activity when making new activities. In the layout xml, you can set the onclick of each checkbox to be the method in the base activity you want executed.
The best practice would be to use a single activity and switch fragments as if they were your screens. Then, the activity could simply implement the listener interface that the fragments would re-use.
Since you have multiple activities this becomes a little bit harder. To really re-use a single listener, I can think of a single (not so beautiful) option. Create a static listener and lazy load it:
public class MainActivity extends Activity {
private static View.OnClickListener sCheckboxClickListener;
public static View.OnClickListener getCheckboxClickListener() {
if (sCheckboxClickListener == null) {
sCheckboxClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Work with v
}
};
}
return sCheckboxClickListener;
}
}
And in each of your activities call:
findViewById(R.id.checkbox1)
.setOnClickListener(MainActivity.getCheckboxClickListener());

Access menu class from MainActivity class in android

I have two different classes. My main activity class and a separate menu.class. I want to be able to get the menu class to load from the mainactivity.class. The menu button does not work on my device or emulator when the mainActivity class is running. I am not sure how to call the menu.class from the mainActivity. I do not want to put the menu java code in the mainActivity.
I am asking for the way to add a snip of code to load a menu activity from one class to another, not to fix my code. I can not find any examples online to show how to load a menu class that is not also written in your main activity class.
public class lifeMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.life_menu);
}
That is a snip of my menu activity. My main activity is called MainActivity
You can make your MainActivity extend your MenuActvity that entends Activity. Hard to know since you did not provide a snip of your menu class.

Functions for an activity in a separate class

I have a MainMenuActivity with multiple fragments. I want to put my code for the fragment "Home" in a separate class. This is my current approach.
public class Home extends MainMenuActivity{
public TextWatcher tw = new TextWatcher() {...};
public Home(){
}
public void buttonPressed(View view){...}
}
I want to add a TextWatcher to a EditText and functions which are executed, when a button is pressed
(defined in the xml: android:onClick="buttonPressed").
If this approach is possible, where and how can I create this class and should I put something in the Home constructor?
You should use Fragments in your project. Here are some examples :
http://developer.android.com/training/basics/fragments/creating.html
http://developer.android.com/training/basics/fragments/fragment-ui.html
For your example you need to change MainMenuActivity to MainMenuFragmentActivity and create Home Fragment.
Best wishes.
With Java you shouldn't be afraid to have very large files (check View.java from the android source code, it's more than 15,000 lines long). There's no way to split a class like there is in C#.
But you say you use fragments. Why is your activity code so large? Usually most of the code go in the fragments, not in the activity.

android common OnItemClickListener and separate button listeners

In my app I have around 12 activities. The first layout of my activity is a list view. By clicking each list it gets redirected to other activities. I have implemented the Listview with OnItemClickListener and is working good.
In all the other 11 activities, I have a common title bar with a logo and a button named as S. When the user clicks on the S button, I am showing the same list of items in the First activity of my app, within the current activities by splitting the page. Now instead of writing a separate OnItemClickListener for each activity, how to write it once and use in multiple activities?
In the same way I have placed 3 button in include layout and I am using this in various activities, how to write a separate common button action, it can be used in multiple activities.
While #SamirMangroliya's method works, this is an alternate method you can use, and the one I've been using for a while.
Create your listener in another class file (say, MyClickListener.java):
public class MyClickListener implements OnItemClickListener {
// This can be OnClickListener, OnTouchListener, whatever you like.
// Implement your method here:
public onItemClick(...) {
// Your selection process, etc.
}
}
Then in each of your Activity objects, all you have to do is:
myObject.setOnItemClickListener(new MyClickListener());
how to write a separate common button action, it can be used in multiple activities.
you should create one Activity(called BaseActivity),
class BaseActivity extends Activity{
#Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
Button btn1 = (Button)findViewById(r.id.btn1);
.
.
.
//now setonclickListner here...
}
}
then extends it ...class MyActivity extends BaseActivity
It sounds like you should implement ActionBar navigation. For a more detailed description about how to do it the Android way, see the Design guide. To support versions of Android between 1.6 and 2.3, link in the ActionBarSherlock library.

Categories

Resources