1

I am building an application in which user logs In, by using username and password, Activities other than LoginActivity must have a logout option. I have visited few threads here on SO regarding it an dhave some idea od how to do that programmatically however I cant think of "where to place this logout button ?".

I would love to have a navigation or Action bar like popular activities i.e. Facebook for Android, Twitter for Android or Google Play store have. Since in future it will be easier to add additional features on this Action / Navigation bar. How should I proceed ? Any blog, tutorial or link will be of great help...

I am targeting Android 2.1+ devices. Here are some threads which I have already read... Using same logout function in all activities

Performing logout

How to logout and many more... As a result got some idea about the back end logic (Using Intent and Intent filters etc.) for logout but can't figure out the UI design.

EDIT In some answers it is suggested to use ActionBar, However as I know it is available only after 3.0+, How I provide such functionality for 2.1+, please help... any tutorial, blog or guide will be of great help.. I don't expect code just some pointers to start with...

Community
  • 1
  • 1
Amit
  • 13,134
  • 17
  • 77
  • 148

2 Answers2

3

One solution would be, include the logout button and if any layout associated with it, within include tags in the layout file. Like

<include> </include>

With this, you at least don't have to create it multiple times.

To call it, in the click listener of the button, use a method with context as the parameter in a Global class. So that, you can call it from all the activities, avoiding the need for rewriting the code again.

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
nithinreddy
  • 6,167
  • 4
  • 38
  • 44
  • I really appreciate the qucik and simple answer you provided(+1), but How can I provide a UI similar to Facebook or twitter where top action bar has a set ofbuttons which all have an important function to do and are available in all the activities... except the login Activity.. Any ideas for that ? – Amit May 14 '12 at 10:13
  • Yes, Why not use ActivityGroup? I haven't used it personally, but AcitivtyGroup is used for that reason. – nithinreddy May 14 '12 at 10:22
2

As already said in your linked post, using a <incude></include> in your layoutFiles to include a little logout button or sth would be a great idea.
Furthermore you could try to build up a BaseActivity that extends Activity and all your other activities are extended from the baseactivity. (what a sentence).

To make sure you dont leak any context you could use the applicationContext (get it via getApplicationContext() in any Activity)

Thkru
  • 4,218
  • 2
  • 18
  • 37
  • +1 for the BaseActivity Suggesttion.. However I can't get the thing about getApplicationContext()... How that can be useful ?? Excuse me if it is a dumb question I am a newbie in android... – Amit May 14 '12 at 10:16
  • no its not ;) ... you need a context to inflate stuff, if you hold a context as a (strong) reference you gonna leak it and your app crashes. To avoid that you could use "getApplicationContext", this context is unLeakable (afaik)... but maybe you dont have to keep an eye on it and the baseActivity stuff just works for you ;) – Thkru May 14 '12 at 10:46
  • How can I build the Action Bar similar to Facebook/Twitter/G+ App for Android (each of them have a Common action bar which provides multiple functionalities... – Amit May 14 '12 at 11:09
  • http://developer.android.com/guide/topics/ui/actionbar.html everything u need to know. ;) upvotes are appreciated. – Thkru May 15 '12 at 09:02