java - Nested classes + extending Main Activity -


basically i'm trying create class processes users login app.
following code have far:

public class loginprocess extends mainactivity {     public class getlogininformation extends asynctask<string, void, string> {         context context = mainactivity.this;           protected string doinbackground(string... params){             /**              * passes information 3 parameters              * @param 1: url              * @param 2: username              * @param 3: password              */             try {                 url url = new url(params[0]);             } catch(malformedurlexception e){                 mint.logexception(e);                 e.printstacktrace();                 log.e("loginprocess", "malformed url");                 toast.maketext(context, "error sending login url", toast.length_long);             }             return "message received";         }          protected void onpostexecute(string response){             toast.maketext(context, response, toast.length_short).show();         }     } } 

the class called calling (for example) new loginprocess().new getlogininformation().execute("http://www.google.com/", "username", "password");

with code, i've attempted extend mainactivity (which main android activity) can application context using mainactivity.this, rather having pass context argument.

i'm wondering whether correct way of doing - having outer class extends main class, , sub class work. @ moment giving me error upon trying show toast.maketext (which example), i'm wondering whether i'm on right track or whether there better way of doing (ie passing context argument or using getapplicationcontext() * etc)

*i have read issues surrounding memory leaks , problems when using getapplicationcontext()

this wrong solution problem. extending class gain access inner state bad idea. it's better rely on composition instead of inheritance, in case need organise how pass dependencies.

if need reference context (or other type of object), pass in constructor. also, if don't need context, don't pass around, prevent memory leaks stated.

create new class, called "datamanager" or that, , let worry data fetching. create instance of class in activity, , pass context through constructor.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -