Event opening

I have an application that stores information about a record in action. in this activity related posts are listed at the bottom of the message. The user, by clicking on the corresponding message, can go to the message and see what message and related messages too.

Picture

As you can see in the image, I have Activity A , which contains messages and related entries. When the user clicks on the message, I send the user Activity A with a new message id and fill the activity with new data.

But I think this is not the right way!

Should I use Fragment instead of Activity ?

+5
source share
7 answers

Opening another instance of Activity on top of another is the easiest way to navigate the content graph. The user can simply click back and go to previously opened content until the user returns to the beginning of the action, then the application closes. Despite a fairly straightforward approach, this particular approach has two problems:

  • It may happen that there are many instances of the same activity on the stack, using a large amount of device resources, such as memory.

  • You have little control over the Activity Stack. You can trigger more actions, complete some, or resort to intent flags, like FLAG_CLEAR_TOP , etc.

There is another approach that reuses the same instance of an Activity, loads new content into it, and also remembers the history of the downloaded content. Very similar to a web browser with web page urls.

The idea is to keep the Stack content still being viewed. Downloading new content pushes more data to the stack, and the top content from the stack is returned back until it is empty. The activity user interface always displays content on top of the stack.

A 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 } } 
+1
source

I would choose:

Activity / fragment depends on complexity:

  • horizontal recyclerview with custom extended map view
  • and inside this extended map view is a second vertical recyclerview :)
0
source

You can try it here.

Create a PostActivity that is a wrapper for fragments. Inside this operation, you can simply replace the fragments with a FragmentTransaction .

Your PostActivity can now have a PostFragment that will contain posts and related posts. Now when you click on the message, you can replace PostFragment with PostDetailFragment with postID , which will be sent to the new fragment as a packet. PostDetailFragment now display the details according to the identifier.

Check here: http://www.vogella.com/tutorials/Android/article.html#components_fragments

0
source

Seeing the image the way I would do it, I would create an activity with a bottom list for your elements, and on top would be the selection of frames for storing fragments. when the user clicks on any element of the list, I would load the corresponding fragment into the action

0
source

It all depends on what you are trying to achieve. What would you expect when a user touches the back button after moving to several levels? If you want the application to exit, no matter how deep in the sequence they went, the best solution in my opinion is to simply reload the same activity with the new data and eliminate the affected views. If you need a back button to return the user to previous data, the next question will be if you are tracking past data. If so, just grab the back button and load the previous data until there is data in your stack, or exit if you get to the top. If you do not want to track the previous data chain, instead of loading a single action with new data, you can start a new action of the same class, but with new data. Android with tracking of actions and each press of the "Back" button will close the current activity and lead the user to the previous activity. The choice of activity compared to the fragment is only yours. You can use fragments that store data that you want to change after each user touch, create new ones when necessary, disconnect previous ones and connect new ones. You will need to do extra work to make sure that the back button works correctly (depending on how you want the back button). Based on what I can say, it’s easier to just have one activity and upload new data as needed and keep track of data changes if you need to go back.

0
source

This can only be achieved through activity. Although I preferred to move all related UI fragments.

You can use the Navigator class.

Here are the steps:
1. Add a navigator class

 public class Navigator { private static Navigator instance; public synchronized static Navigator getInstance() { if (instance == null) { instance = new Navigator(); } return instance; } public void navigateToActivityA(Context context) { Intent activity= AActivity.getCallingIntent(context); context.startActivity(activity); } } 

2. Add a call method to the Activity class.

 public static Intent getCallingIntent(Context context) { return new Intent(context, AActivity.class); } 

3. Call the action with the following code in the activity of your caller.

 Navigator.getInstance().navigateToActivityA(this); 

I suggest you read AndroidCleanArchitecture

0
source

For this task ...

0) Start a new activity

I read about the question again and realized that you need advice to get started. So, starting with a new activity, well, your main problem will be on the other side (see below).

But let's talk about the start of other data. Using Fragment instead does not solve your problem, fragments help when working with other screens. Using, for example, just updating data as an option. You can use only one activity and update only data, it will look much better if you also add animation, but no better than getting started.

Using a snippet helps you with various screen actions. And, perhaps, answering your question - this will be the most suitable solution . You use only one activity - PostActivity and several fragments - FragmentMainPost, FragmentRelated - which will be replaced by each other, choosing from the corresponding record.

1) Return problems

Suppose users click on a new action and upload new data. This is normal, and when users click more than 100 actions and get a lot of information. Good too. But the main question here is going back (also about caching, but let's leave it for now).

So everyone knows this is a bad idea to keep a lot of action on the stack. Therefore, for each of my applications with similar behavior, we override onBackPressed in this exercise. But how, let's see the stream below:

 //Activities most have some unique ID for saving, for ex, post number. //Users clicks to 100 new activities, just start as new activity, and //finish previous, via method, or setting parameter for activity in AndroidManifest <activity noHistory=true> </activity> .... //When activity loaded, save it activity data, for ex, number of post //in some special place, for example to our Application. So as a result //we load new activity and save information about it to list .... // User now want return back. We don't save all stack this activities, // so all is Ok. When User pressing back, we start action for loading //activities, saved on our list.. ..... onBackPressed () { //Getting unique list LinkedTreeSet<PostID> postList = getMyApplication().getListOfHistory(); //Getting previous Post ID based on current PostID previousPostID = postList.get(getCurrentPost()); //Start new activity with parameter (just for ex) startActivity(new Intent().putExtra(previousPostID)); } 

RESULT

I found this the best solution for these tasks. Because at every moment - we work with only one activity!

0
source

All Articles