android - Opening Instance of Activity -


i have app hold post information in activity. in activity related posts listed in bottom of post. user clicking on related post can go post activity , see post info , related posts too.

figure

as can see in image, have activity a holds post , it's related posts. when user click on post send user activity new post id , fill activity new data.

but think not right way!

should used fragment instead of activity?

opening instance of activity on top of simplest way of navigating content graph. user can press back, , go opened content, until user reaches starting activity, application closes. though pretty straight forward, particular approach has 2 issues:

  1. it may happen lot of instances of same activity on stack, utilising large amount of device resources memory.

  2. you don't have fine grained control on activity stack. can launch more activities, finish some, or have resort intent flags flag_clear_top etc.

there approach, re-uses same activity instance, loads new content in while remembering history of content loaded. web browser web page urls.

the idea keep stack of content viewed far. loading new content pushes more data stack, while going pops top content stack, until empty. activity ui displays content top of stack.

rough example:

public class postactivity extends appcompatactivity {     // keep history of viewed posts, current post @ top     private final stack<post> navstack = new stack<>();      @override     protected void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // starting link intent extras , call loadpost(link)      }      private void loadpost(string link){         // load post data in background , call onpostloaded(post)         // called when user clicks on related post link      }      private void onpostloaded(post post){         // add new post stack         navstack.push(post);          // refresh ui         updatedisplay();     }      private void updatedisplay(){          // take top post, without removing stack         final post post = navstack.peek();          // display post data in ui     }      @override     public void onbackpressed() {         // pop top item         navstack.pop();          if(navstack.isempty()) {             // no more items in history, should finish             super.onbackpressed();         }else {             // refresh ui item on top of stack             updatedisplay();         }     }      @override     protected void ondestroy() {         super.ondestroy();         // cancel background post load, release resources     } } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -