What is the best way to push replace with arguments in flutter? - android

I'm trying to find out the best routing solution for this scenario here. I have screens with workflow as follows.
Items Screen(1) ===> Item Details Screen(2) ====> Edit Items Screen(3)
On the Edit Item screen, when I edit things and save, it should take the user back to the Item details screen with some objects and variables passed as routing arguments.
I used
pushReplacementNamed("/details", arguments: ItemDetailsArguments(item: item, variables: variables)); on a button click from the edit item screen.
This works fine, until I noticed when I click on the back button on the App bar (Item Details Screen(2)) it takes me all the way back to the Edit Item Screen(3) again instead of the Items Screen(1).
Is there a better way to pass data from screen 3 to 2 upon button click and then take the user all the way back to screen 1 when the back button is pressed from screen 2?

Instead of a pushReplacementNamed you can use pushNamedAndRemoveUntil with a predicate to remove the unwanted routes from the stack.
Read more about pushNamedAndRemoveUntil
pushNamedAndRemoveUntil(
"/details",
ModalRoute.withName("/items"), // screen one - route predicate
arguments: ItemDetailsArguments(item: item, variables: variables),
);
Route predicate will let you decide what needs to be removed from the stack.
In this case, after the button is clicked, push to the "details screen" and also remove all other routes that are in the stack til the main screen. Hope it helps!

Related

Change displayed item in Stack View widget

I have a stack view widget can hold a variety of items. When I select an item I want it to go to the "front" of the list. I've currently been able to implement this.
The problem I'm having, is that I can't seem to figure out how to make the StackView "reset" itself or go back to displaying item 0. So what ends up happening is the user selects an item in the widget, but when they come back they have to manually scroll the widget back to item 0 to see that item since it's moved.
Is there any way to force the Stack View widget to "advance" or "move" to position 0? I've tried numerous different methods from the documentation but none seem to do the trick.
Thanks!

Efficient way to display details of list item

I am working on app that is targeted to 7" or above tablets. So target platform is Android 3.x.
I am looking for an efficient approach to display details for list item when it is clicked. I have a list displayed with bunch of items. (Note: Due to the nature of the application I do not want to share ListView with other view or Fragment in this activity). When an item of list gets clicked I have to display details of that time. Details of an item take pretty much whole screen. I have couple of approach in mind:
Simplest one is : on List item clicked simply start an Activity that displays details of that item. But I guess it didn't seem efficient as every time item is clicked this Detail Activity is created and then destroyed.
Create a custom dialog for Detail of list item and hold its reference in List Activity. As soon as item is clicked show this dialog displaying contents corresponding to clicked list item. With this approach I would like Dialog to take complete screen (any suggestion appreciated).
Define FrameLayout in ListView with Visibility Gone (so that frame layout dont take any space in ListView screen). This frame layout act as a container for Details Fragment. As soon as list item is clicked hide ListView and make frame layout visible. With this approach I am not efficiently navigate back and forth between list view and details view.
Should come from an Android expert. :)
Thank you for your time.
There isn't any reason why you can't take approach 1 (create an Activity to display the details). Even better combine 1 & 2 to create an Activity with a dialog theme with something like this in the AndroidManifest.xml...
<activity
android:name=".DetailsActivity"
android:theme="#android:style/Theme.Dialog">
</activity>
Give the Activity a 'Close' button which when clicked, calls finish() to exit the Activity. I have several dialog-themed Activities in my current project and they work really well.

highlight previous selected item when user come back from another activity

Activity1 has a listView. Clicking one item (let's say item 3) will start Activity2. Activity2 have a back button once clicked will bring user back to Activity1. What I want to achieve is to highlight the item 3 when user back to Activity1 so that the user have a sense where to continue. (May be i need to set the focus to item 3 as well.)
EDIT: Following code works.
public void onResume()
{
super.onResume();
//lastSelectedPosition saved in OnItemClickListener
lv.setSelection(lastSelectedPosition);
lv.requestFocusFromTouch();
}
well its pretty simple. just save the clicked item position of the list to a field when the list is clicked to launch your new activity.
Afterwards in the onResume() method just use myList.setSelection(savedPosition);
as for the highlighting, well focus works kinda bad especially if you have a bit more complex rows(buttons,checkboxes etc) and other ui elements beside the list that can take away the focus. i believe the best way to achieve this is just set the background of that particullar item onResume to the highlighted one and override onScroll listener to just change the background to your default when the list is scrolled. indeed its a workaround but it will work in 100% of the cases opposite to just focusing an item. plus maybe you can add animations on the view so you can make it look really nice and smooth.

Android -- First ListView item is sometimes selected when ListView is initially displayed

When I press Menu and the ListView is presented, the top item is usually highlighted when the Activity is first started. I can click and make it not highlighted. What might be causing this? Actually I don't care about that, I just need a hack to stop it. I would post a code snippet but the ListActivity and its xml exceed 600 lines and I have no idea where the problem lies. Any ideas?
The answer can be pretty easy: The first entry of every ListView is highlighted, when you are in the "non touch mode". That means when you navigate with the trackball.
Test it: Go the the home screen. Press the menu button and select "settings" with the trackball. The list displaying the settings main menu is now highlighted in the first row.
Doing the same without the trackball but with a touch on the menu entry "settings" the new list should be not highlighted.

Displaying Alert Message Box on click event

I have an application where I have Tab Host. I have certainly four tabs in my Tab Host and one of my tab contains a list of some elements. Now When a user click on the element of the third Tab he is supposed to switched to the first tab and the data of the list element gets displayed there. But when the user initially tries to click on the first Tab without selecting any element from the list of third tab, I need to display the Alert Message that "Please select an item form the List"
I wonder how to do that particularly?
Thanks,
david
use AlertDialog.
Toast.makeText(this,"Please select an item form the List",Toast.LENGTH_LONG).show();
use it on your click event.
You can use any of the above, but I feel its the logic that you're more interested in.
You might be having four different activities for each tab.
Create on more class, which will serve as your Bean class, which holds all the data to transfer information between classes.
Call this class as PrefBean.
Make all its variables static (as of now you'll be using only one to know whether user has selected any list item, and if yes, which one). This way, the variables will be globally available to all of your activities.
Have an integer in PrefBean which depicts whether something is selected or not in your third tab.
The logic goes as this:
Initially, your integer in PrefBean will hold something less than zero (say -1). This will show that nothing is selected as of then.
When user clicks in the first tab, your first activity will be invoked and it should check the value of that integer in PrefBean, display an error message to the user. If the value is negative, means nothing is selected, if its positive, it will give you the position of the row selected. Load anything depending on the row position selected
When user clicks on any row in your third tab's list activity, set your PrefBean integer == the row position selected.
I hope you got the logic

Categories

Resources