Activity- OnbackPressed issue - android-listview

Am launching new activity like below
Intent mIntent = new Intent(getActivity().getApplicationContext(), RetailerHomeActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mIntent);
This activity having listview, whenever listview item click am replacing this listview with fragment view.
but after press home button and wait for sometime, if i try to go back activity listview by onbackpress listview, adapters became null
how to avoid this situation.?

Try this way , you will not get null.
recall your adapter onRestart() in your activity.
#Override
protected void onRestart() {
super.onRestart();
GetListData(); // call your adapter here or set data of listview here too
}

Related

Open dialog box in Flutter Stateless widget using ChangeNotifierProvider

I have a Stateless flutter widget which shows a list of TODOs. The list is shown using a ChangeNotifierProvider (of TodoList object, which contains a list of todos). Now when I load the page, I want to show a dialog box asking user to enter a new TODO if and only if the existing todos is empty. Inside the builder of the ChangeNotifierProvider, i tried below logic
if (todoList.todos.length == 0) {
_showDialog(context);
return Column...;
} else {
return ListBuilder...;
}
But its showing 2 dialog box (probably due to the build method executing twice). I have to pass context to dialog box because I'm updating the todoList inside it, which should trigger a rebuild.
How do I handle this scenario. I've tried using flag (_isDialogOpen) but its not still working?
make the widget Stateful in order to use it's lifecycle methods, you can use then initState() for showing the dialog when the page widgets are inserted in the widget tree, but you will need to use an addPostFrameCallback() to schedule showing it 1 frame after the initState's code gets executed:
First, import:
import package:flutter/scheduler.dart.
Then use this:
#override
void initState() {
// ...
SchedulerBinding.addPostFrameCallback((_) => _showDialog(context),);
}

Is there a way to execute a method which is under a Button Click?

I use Flutter. I have a method inside a button click event which executes.
I want to execute that method as soon as the dart screen opens just once. I cannot put the method inside the widget build as it would execute everytime the state is refreshed. Is there a way to execute button click event without the user actualy clicking on the button?
EDIT : I’m aware that I can use a boolean or some variable and set it to false after executing once. I was just looking for a way around that.
Just call it in the initState method, like this:
#override
void initState() {
yourMethodOnClick();
super.initState();
}
This is in case you are using it StatefulWidget
Either you use StatelessWidget You should call the method in the contractor, like this:
YourClass extends StatelessWidget{
YourClass() {
yourMethodOnClick();
}
}

Flutter - keep listen a stream on page change

My scenario is like this:
a BottomNavigationBar where one page contains a list of items ("listPage") and another page is a single item ("itemPage").
both pages can navigate to "itemPage" related to a different items.
the "itemPage" show details of the product and have a "Favourite" toggle button.
in the "listPage" every item shown via ListView.builder, show and can handle the "Favourite" toggle button.
I can't understand how reflect the "Favourite" change in an "itemPage" to others "itemPage" (if opened multiple time for the same item, yes, it's possible) and also to the same item in the "pageList".
I created a NotifierBloc where a BehaviorSubject > Sink is called every time a "Favourite" toggle button change its state (putting the itemId and the boolean value of the Favourite). After a database update an output PublishSubject > Stream is filled with the additional information of the item.
In this way every time a "Favourite" is toggled, all the subscribers receive the info about the flag.
BehaviorSubject<Item> inController = new BehaviorSubject<Item>();
Sink<Item> get putUpdate => inController.sink;
final PublishSubject<Item> outController = PublishSubject<Item>();
Stream<Item> get getUpdates => outController.stream;
NotifierBloc() {
inController.listen(_handleToggle);
}
_handleToggle(Item item) {
...
outController.sink.add(newItemAfterDatabaseCall);
}
In ListPage and ItemPage (both StatefulWidget) I created a StreamSubscription in DidChangeDependencies method which listen to the NotifierBloc Stream.
The StreamSubscription will be cancelled in the Dispose method.
StreamSubscription _subscription;
void didChangeDependencies() {
super.didChangeDependencies();
_subscription = _notifierBloc.getUpdates.listen((item) => {
// Do things with the Item like setState or call bloc methods
...
});
}
void dispose() {
_subscription?.cancel();
super.dispose();
}
Problems are when a new itemPage is opened: the dispose method cancel the subscription in the previous shown page, so new events will not be get listened.
In addition when a page is shown due to a previous page close, the StreamSubscription is renewed and I get updates about the last one Favourite change, but I need a list of Favourite changes, because maybe the user opened several "itemPage"s.
How can I solve?
Maybe the Stream must be passed to the Page(not the PageState)? But how to handle onData function?
You need some sort of state management system. Look into Provider. Here is a simple example of how you could use it. https://github.com/m-Skolnick/provider_example_flutter

Why is my Widget not updated after preferences are set? [duplicate]

The PreferenceActivity seems like a really easy helper class to manage the preferences of an app.
I'd like to also use that same class from the widget.
On the first instance of a widget I want the PreferenceActivity to come up. This was very easy to do from the configuration activity but here is the glitch...
I can't tell when the preference edit is completed!
All examples of widget preferences I see on the net use a manually built preference screen rather than the PreferenceActivity helper class and then listen for the 'save' button being clicked.
How can I do that with the PreferenceActivity since that activity doesn't have a save button. You just use the 'back' button to commit your preferences changes.
Thanks!
I have been trying to do the same thing and I think I've cracked it. I handle the onBackPressed() event in the PreferenceActivity and perform a widget update from there using sendBroadcast().
In PreferenceActivity:
#Override
public void onBackPressed() {
Intent intent=getIntent();
Bundle extras=intent.getExtras();
int widgetId=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// this is the intent broadcast/returned to the widget
Intent updateIntent = new Intent(this, WidgetProvider.class);
updateIntent.setAction("PreferencesUpdated");
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
sendBroadcast(updateIntent);
}
In your WidgetProvider:
#Override
public void onReceive(Context context, Intent intent) {
if ("PreferencesUpdated".equals(action)) {
// update your widget here
// my widget supports multiple instances so I needed to uniquely identify them like this
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
updateWidgetView(context, remoteView, appWidgetId);
}
}
NOTE: In 1.5 onBackPressed isn't support so comment out the #Override for onBackPressed and add this code
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(LOG_TAG, "onKeyDown() "+ keyCode);
if (keyCode==KeyEvent.KEYCODE_BACK && Integer.parseInt(Build.VERSION.SDK)<5) {
onBackPressed();
}
}
I should add that I'm new to Android development so I may be doing this entirely wrong. All I can say it is works for me :)
Let me know how you get on.
You should be able to tell when the Preference edits are complete by implementing a SharedPreferences.OnShredPreferenceChangeListener. Using this, you could update only when certain keys are changed, or unconditionally when any are changed. Remember to register it in your PreferenceActivity onCreate and unregister it in the onDestroy.
In this case, you can do something similar to Android: How do I force the update of all widgets of a particular kind within the listener to cause all of your widgets to update based on a SharedPreference change.
It's an old question, so this may not be relevant anymore.

Using PreferencesActivity from a widget?

The PreferenceActivity seems like a really easy helper class to manage the preferences of an app.
I'd like to also use that same class from the widget.
On the first instance of a widget I want the PreferenceActivity to come up. This was very easy to do from the configuration activity but here is the glitch...
I can't tell when the preference edit is completed!
All examples of widget preferences I see on the net use a manually built preference screen rather than the PreferenceActivity helper class and then listen for the 'save' button being clicked.
How can I do that with the PreferenceActivity since that activity doesn't have a save button. You just use the 'back' button to commit your preferences changes.
Thanks!
I have been trying to do the same thing and I think I've cracked it. I handle the onBackPressed() event in the PreferenceActivity and perform a widget update from there using sendBroadcast().
In PreferenceActivity:
#Override
public void onBackPressed() {
Intent intent=getIntent();
Bundle extras=intent.getExtras();
int widgetId=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// this is the intent broadcast/returned to the widget
Intent updateIntent = new Intent(this, WidgetProvider.class);
updateIntent.setAction("PreferencesUpdated");
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
sendBroadcast(updateIntent);
}
In your WidgetProvider:
#Override
public void onReceive(Context context, Intent intent) {
if ("PreferencesUpdated".equals(action)) {
// update your widget here
// my widget supports multiple instances so I needed to uniquely identify them like this
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
updateWidgetView(context, remoteView, appWidgetId);
}
}
NOTE: In 1.5 onBackPressed isn't support so comment out the #Override for onBackPressed and add this code
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(LOG_TAG, "onKeyDown() "+ keyCode);
if (keyCode==KeyEvent.KEYCODE_BACK && Integer.parseInt(Build.VERSION.SDK)<5) {
onBackPressed();
}
}
I should add that I'm new to Android development so I may be doing this entirely wrong. All I can say it is works for me :)
Let me know how you get on.
You should be able to tell when the Preference edits are complete by implementing a SharedPreferences.OnShredPreferenceChangeListener. Using this, you could update only when certain keys are changed, or unconditionally when any are changed. Remember to register it in your PreferenceActivity onCreate and unregister it in the onDestroy.
In this case, you can do something similar to Android: How do I force the update of all widgets of a particular kind within the listener to cause all of your widgets to update based on a SharedPreference change.
It's an old question, so this may not be relevant anymore.