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.
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.
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:
With first approach you will run into problems when you'll need to extend existing Activities in Android (eg ListActivity, TabActivity, etc.)
Check this answer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.