how to apply master page concept in android application? - android

I want common logout action to all pages in android application.i have common template that contain logout option. but i repeat the logout function to all activity. how to resolve this problem.

The simplest thing would be to extend a common Activity, as someone else suggested. You are able to extends only from one class, that's why you would do something like this:
public class CommonActivity extends Activity {
// Here you implement log out methods
}
public class ParticularActivity extends CommonActivity {
// Here you put your particular class variables and methods
}
This way, you have the logout functionality in every activity, all you have to do is extends the common one.

Create a Base activity which extends Activity. Write the logic for logout in it. Then extend this class in all activities of the app.

You can either:
extend Activity class and implement the functionality there, your activities would extend this class to provide same functionality
use delegation pattern - create some helper class which contains needed functionality
With first approach you will run into problems when you'll need to extend existing Activities in Android (e.g. ListActivity, TabActivity, etc.)
Check this answer.

I have done this using XML file.
I am just creating runtime view from XML file , and add it to the Activity layout.
I have created method for that
public static void setLoginview(Context ctx, RelativeLayout layout) {
LayoutInflater linflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = linflater.inflate(R.layout.loginheader, null);
layout.addView(myView);
try {
layout.getChildAt(0).setPadding(0, 50, 0, 0);
} catch (Exception e) {
}
}
ctx is the application contetx and layout is the layout in which i want to add that view.

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;

Show View in all my app

I need show a TextView in all Activities, but is much work to do it one by one, because I have +10 Activities.
My objective is when I click in a button, show a textview ("Importing ...") at the bottom of the application. This textview will disappear when I receive a push notification, and I owe a pop up with the response (the pop up also has to appear in any activity).
My project has a custom abstract BaseActivity and all activities extends it.
public abstract class BaseActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void setActionBar(#IdRes int idResToolbar) {
Toolbar toolbar = (Toolbar) findViewById(idResToolbar);
setSupportActionBar(toolbar);
updateFont(toolbar);
}
// ...
}
I think I could use for my purpose but not how to do it.
If anyone has any suggestions I will be happy to hear it.
Thanks in advance.
Use fragments for your content (instead of different activites) you then can add global views to the activity, which holds the fragments.
If you don't want to do that, you'd have to modify the layout(s) in your Base class.
I would suggest you to use a PopupWindow that contains the text view and create a separate class that initializes the PopupWindow on the basis of context given to it.
Now in all your Activities you will have the control of showing and hiding the window as you want. Make sure to make all utility methods required in the separate class to avoid coherence for example hiding and showing the window. setting text of text view of the window and etc.
You can write in onCreate() of your base activity something like
setContentView(R.layout.base_layout);
And in every other Activity at start of onCreate() method, just use super.onCreate()
And more than that to support different layouts add something like this in onCreate() (example for one of activities)
LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.activity_1_layout,rootGroup)
where rootGroup is a ViewGroup in your Base Activity, in which you will add additional components for every other activity
Create a service, which creates a View which can be drawn over other apps (will require the relevant permission in the manifest)
You could use one of the open source libraries available like this or refer to this example
It's better you use fragments instead of using many activities. However, if you don't wanna do so, I suggest you create a factory which will generate a textview to all activities. Then you must add it into each activity's view.

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());

What is the difference from inflating an activity and inflating a view in android?

I don't seem to understand if it's possible to inflate (include) an activity into another activity. I know i can inflate a layout xml, this works, but i am wondering if i can inflate an activity. For instance , i have class A that extends Activity and another class B that extends ListActivity. Can i include and use in class A, my class B?
THis is what i have tried:
Class A:
LayoutInflater inflater = (LayoutInflater) MyActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate list
BActivity list = new BActivity();
Class B:
public class BActivity extends ListActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
List<Model> models= new ArrayList<Model>();
models.add(new Model("John"));
models.add(new Model("Cage"));
setListAdapter(new MyAdapter(this, models));
ListView list = getListView();
}
}
and in xml (the class A xml): (for where i want to see the list)
<view class="com.test.BActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > </view>
All of this throws errors :
Error inflating class BActivity
The activities are declared in the manifest.
Do you know what i am doing wrong? this is not the correct way to inflate another activity? I am using Android 2.2 api 8.
Thank you for your time.
Your question title and your issue are not actually the same thing. For completeness, I will answer both.
What is the difference from inflating an activity and inflating a view in android?
The answer is there is no difference. Ultimately, they are the same in process and logic. However, an Activity may have many different Views and you may setContentView() several times to several different Layouts or Views based on your need. An Activity requires a Layout resource, and a View may or may not be a Layout.
Do you know what i am doing wrong?
Yes. Absolutely.
Your code: BActivity list = new BActivity(); is not actually inflating an Activity. You are constructing the Activity, but not starting it.
Your XML defines BActivity as a View, but your code defines it as an ListActivity. These are two different things entirely. A ListActivity has a ListView (extended or otherwise); A ListActivity is not a ListView.
Activity and its subclasses are Contexts that have a Life Cycle that is managed by the OS. They contain and speak to Views of all types, but are not themselves Views.
this is not the correct way to inflate another activity?
No sir, but fear not! The answer is not too far away.
FAKE ANSWER (for completeness) -
First, to start another Activity so that it is inflated, you must call startActivity() from a Context. A Context may be an Application, Activity, Broadcast Reciever or any other app component (Component = declared object in your Android project manifest). So, if you really wanted to start a new Activity, you would change BActivity list = new BActivity(); to:
Intent _listActivity = new Intent();
_listActivity.setClass(BActivity.class);
startActivity(_listActivity);
REAL ANSWER -
However, since you want to see your List in class A, BActivity is not an Activity, it is a View. That means what you REALLY want is to make it recognize your View and this is a different solution. Change public class BActivity extends ListActivity to public class BActivity extends ListView and now all of a sudden you have a custom View!! Now all we have to do is get the List to work.
Constructing the View - Views are different from Activities in that they do not have a public void onCreate(Bundle bundle). All of your stuff from BActivity.onCreate() would instead be placed in the constructor. But, you don't have a proper constructor... hmmm. Well, there are three constructors to choose from -- add one or all of the following (You will probably want either option 1 or 2, at first. But you won't use both at the same time hint hint, read the comments:
//This constructor is used when the View is created from code (not XML!!)
public BActivity(Context context)
{
}
//This constructor is used when the View is created from XML (not code!!)
public BActivity(Context context, AttributeSet attr)
{
}
//This constructor is used when the View is created from XML with a Style defined in separate XML.
public BActivity(Context context, AttributeSet attr, int defStyle)
{
}
Inflating the Activity = Inflating the View
You have a choice here, you can either add the View, or you can inflate the View. There are many options for both. Based on your question, I shall assume you want to inflate the View. Simply change BActivity list = new BActivity(); to setContentView(R.id.MyXML). MyXML, of course, would be the name of your XML Layout file. SetContentView will then open the appropriate View for you (BActivity) using the 2nd constructor from the list above.
Understanding the difference between View and Activities is important. The processes between them are very similar, but they themselves have a intertwined but separate purpose.
An Activity MUST have a View.
A View MUST be in a Context.
An Activity is a Context, but a Context may also be one of several other possible classes.
Both may be inflated using a LayoutInflater
An Activity has a convenience method called setContentView which can inflate an entire XML file.
A View must inflate each View manually using LayoutInflater object.inflate().
An Activity has a Life Cycle. A View has a draw cycle instead.
For more information, certainly read more on the Android Developers Resources. However, some of these things are only learned by experimentation.
Hope this all helped!
FuzzicalLogic
Inflating means parsing an XML description of an object and building a Java object with the described attributes. Activities do not have a detailed description in XML and thus no point to inflate them.
You can dispatch activity A from activity B or you can use the new concept of Fragment to combine multiple sub activities into activities.
BTW, you can define custom views (by subclassing one of the view classes) and use it in your XML layouts, just put the class full path e.g. in the layout instead of let say .

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

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

Categories

Resources