Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Instance of Activity

I have an app that hold post information in an activity. in this activity related posts listed in bottom of post. User by clicking on related post can go to post activity and see that post info and related posts too.

Figure

As you can see in image, I have Activity A that holds post and it's related posts. When user Click on post I send user to Activity A with new post id and fill activity by new data.

But I think this is not Right way!

should I used Fragment instead of Activity?

like image 425
MAY3AM Avatar asked Nov 08 '22 14:11

MAY3AM


1 Answers

Opening another Instance of an Activity on top of another is simplest way of navigating a content graph. User can simply press back, and go to previously opened content, until user reaches back to starting Activity, then the application closes. Though pretty straight forward, this particular approach has two issues:

  1. It may happen that a lot of Instances of same activity are on the stack, utilising a large amount of device resources like memory.

  2. You don't have a fine grained control over Activity Stack. You can only launch more activities, finish some, or have to resort to intent flags like FLAG_CLEAR_TOP etc.

There is another approach, that re-uses the same Activity instance, loads new content in it while also remembering the history of content that was loaded. Much like a web browser does with web page urls.

The Idea is to keep a Stack of content viewed so far. Loading new content pushes more data to stack, while going back pops the top content from stack, until it is empty. The Activity UI always displays the content from top of the stack.

Rough Example:

public class PostActivity extends AppCompatActivity {
    // keep history of viewed posts, with current post at top
    private final Stack<Post> navStack = new Stack<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get starting link from intent extras and call loadPost(link) 
    }

    private void loadPost(String link){
        // Load post data in background and then call onPostLoaded(post)
        // this is also called when user clicks on a related post link 
    }

    private void onPostLoaded(Post post){
        // add new post to stack
        navStack.push(post);

        // refresh UI
        updateDisplay();
    }

    private void updateDisplay(){

        // take the top Post, without removing it from stack
        final Post post = navStack.peek();

        // Display this post data in UI
    }

    @Override
    public void onBackPressed() {
        // pop the top item
        navStack.pop();

        if(navStack.isEmpty()) {
            // no more items in history, should finish
            super.onBackPressed();
        }else {
            // refresh UI with the item that is now on top of stack
            updateDisplay();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // cancel any background post load, release resources
    }
}
like image 106
S.D. Avatar answered Nov 14 '22 21:11

S.D.