Close PreferenceFragment after widget configuration complete - android

I am using a PreferenceFragment to configure my widget when it is added to the home screen. After the user has edited the preference to their likings, how can I close the preference fragment so the widget can then be added based on the settings?
I was thinking of using an "Add Widget" preference at the bottom of the fragment, and adding an onClickListener. But am at a loss as what to do programmatically after the user clicks this. I was thinking something like a finish() method, but this only works on Activities.
All help is greatly appreciated.

As per the App Widgets guide: You must create an Intent, add the app widget Id as an extra (with the AppWidgetManager.EXTRA_APPWIDGET_ID key), call setResult and finish. If it's a fragment, use getActivity() to call these methods on the Activity.
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, result); // or getActivity().setResult(RESULT_OK, result);
finish(); // or getActivity().finish();

Related

Start a new instance of an activity from the same activity in android

I am trying to create a new instance of an activity from within the same class of the activity. For instance I have a Activity called Settings.
Intent intent = new Intent(Settings.this, Settings.class);
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("ShowBookmarks", true);
startActivity(intent);
And in my AndroidManifest I don't have any launch options set, though I have tried with singleTop and a couple others.
The goal is to display different data in the same controls (Recycler view, etc), and then be able to click back to show the previous data.
Intent intent = new Intent(Settings.this, Settings.class);
I don't know if that's possible | Re-think your strategy
But, if you want to just restart your activity:
You don't have to write those long things.
Just call onResume(); from anywhere.
I do that in this way.
Also,
Since API level 11, you can call the recreate(); method of the Activity.
Create two Fragments or you can manage using one fragment means go with one fragment.
Add the fragment into backstack (addToBackStack(String name)) with different name.
Pop the fragment in onBackPressed() method.
It will work as you expected.
Note: All your activity related code move into Fragment.

Android how to open Activity when open home screen widget?

I am implementing an app related to widgets in this I prepared one widget and that's working perfectly and when click on views that opens the activity to prepare settings for that widget.
But my requirement is when click on home screen widget I want to open directly activity and then widget.
From the above picture when click on Custom Analog Clock widget open first Activity in my application and then I want to show the widget because from my activity I will setup some settings for that widget. How can I open activity first?
Widgets can have a special activity called when they are added to the home screen. That activity can return a SUCCESS intent with the widget ID receive in the "onCreate" method of the activity or RESULT_CANCELED if you want to prevent the creation of the widget itself.
You just need to add the configure activity class in a android:configure attribute at the appwidget-provider XML and add an intent filter with android.appwidget.action.APPWIDGET_CONFIGURE to your activity declaration at the AndroidManifest. It's detailed in http://developer.android.com/guide/topics/appwidgets/index.html#Configuring
The intent received has the widget ID:
public void onCreate(Bundle savedInstanceState) {
...
int widgetID = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
...
}
and then you can either finish your activity with setResult(RESULT_CANCELED); or confirm the widget creation with:
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_OK, resultValue);
You can do all your widget settings from that activity.
On the other hand, if you want to perform other setup that only needs to occur once for all your widgets instances, you can make then in the onEnabled method of your AppWidgetProvider class as described in http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider
You should check out this tutorial: http://android-er.blogspot.nl/2010/10/simple-home-screen-app-widget-with.html It does exactly what you want.

Android Crashing on user logout

I have the following code which is executed once the user clicks a button
public void logout(){
// redirect user back to login screen activity
Intent i = new Intent(this, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// start Login Activity
startActivity(i);
}
However each time the button is clicked, the emulator crashes. Any ideas what i might be doing wrong?
You can set flags instead by doing
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
I doubt this is your issue though, make sure you are calling startActivity from within the Activity and the correct Thread, and make sure there are no issues with the onCreate of your LoginActivity.
Is this consistent with more than one emulator? Try a different configuration, and also check your AndroidManifest.xml file, is the second Activity defined?
The issue was prior to this method being called. I was trying to set the text on an EditText item that was part of another layout

Refresh activity and re-open

This is my first post, so please be nice :)
I have a question and no one gave the answer in the post I've seen.
My app has a list and a button to open an activity, this activity creates a new item to show in the list of the previous activity when pressing the button Create.
How can I do this?
This is the code I made for the first button:
intent = new Intent(this.getBaseContext(), NewTestActivity.class);
this.finish();
startActivity(intent);
and this is the code to go back an refresh:
Intent intent = new Intent(getBaseContext(), TestListActivity.class);
startActivity(intent);
But the code to goback is useful, because the activity don't refresh.
I have to call the new activity in a diferent way? Or go back to previus activity in a diferent way? Or go back normally and refresh the activity when I'm back in the previus activity?
Well...this is all.
Sorry for my bad english and, if this question has been answered in another thread, give me the link to read, because I can't find it :)
PS: I started with android in December.
Thanks for your help.
When you are going to start new activity you shouldn't close a current one until you actually need this behaviour. (remove this.finish(); row from your code)
Also you shouldn't close an active activity manually until you actually need it. When the user presses the "back" button on the device Android pops the previous activity from the "back stack". Read once more Activity documentation.
According to your description you have a list of elements. So in order to refresh the list you need to update your dataset and notify the list about it by ListView.notifyDataSetChanged() method call.
Before getting to a real answer, I'd like to show some improvements to your code.
First, when creating an Intent (or when you need a context in general) from an Activity there is no need to call getBaseContext(), you can just use this:
intent = new Intent(this, NewTestActivity.class);
Second, android is good at handling Activities, you do not have to close your first activity manually with finish(). Android will automatically pause or stop your first activity and bring it back when you return to it.
Third, in your case you might want to use startActivityForResult() instead of startActivity() for reasons I will explain below.
This will make your code look like the following:
private static final int MY_REQUEST_CODE = 33487689; //put this at the top of your Activity-class, with any unique value.
intent = new Intent(this, NewTestActivity.class);
startActivityForResult(intent, MY_REQUEST_CODE);
Now, the startActivityForResult() starts an activity and waits for a result from that new Activity. When you call finsih() in the new Activity you will end up in the first Activitys onActivityResult()-method, with data supplied from the new Activty that is now closed:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != MY_REQUEST_CODE) return; //We got a result from another request, so for this example we can return.
if(resultCode != RESULT_OK) return; //The user aborted the action, so we won't get any data.
//After the above if-statements we know that the activity that gives us a result was requested with the correct request code, and it's action was successful, we can begin extracting the data, which is in the data-intent:
Item item = (Item) data.getSerializableExtra("customData"); //casts the data object to the custom Item-class. This can be any class, as long as it is serializable. There are many other kinds of data that can be put into an intent, but for this example a serializable was used.
itemList.add(item); //This is the list that was specified in onCreate()
//If you use an Adapter, this is the place to call notifyDataSetChanged();
}
For this all to work, we need to do some things in the second Activity:
When the item has been created, we must set a result:
//We begin by packing our item in an Intent (the Item class is an example that is expected to implement Serializable)
Item theCreatedItem; //This is what was created in the activity
Intent data = new Intent();
data.putSerializable(theCreatedItem);
setResult(RESULT_OK, data);
finish();
This should return to the first Activitys onActivityResult()-method with the item, as explained above.

Returning to the Home Screen and then returning to last activity

Having real issue understanding how to sort my issue out.
On the Home screen I have 2 buttons
When the user clicks the first button it starts a new Activity. What I am looking for is if the user clicks back the app returns to the home screen. If the user clicks the first button again it starts a new activity.
If the user clicks the second button it returns to the activity that was last started by clicking button 1
What I am having issue with is how to save the state of the activity when the user clicks back
Also how to call that activity when the second button is pressed
Thanks for your Time
UPDATE
I have gone down part of this but still having issues. If I put some of the code I am using perhaps someone can point where I gone wrong.
Code for calling the new activity from main menu
Intent intent = new Intent(MainMenu.this, NewClass.class);
intent.putExtra("value1", value1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Within the new class I have added the following :
#Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(RoundScoring.this, MainMenu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Toast.makeText(this, "Back Button Pressed", Toast.LENGTH_LONG).show();
}
I do not have either a onrestoreinstancestate or onresumne in this class. only a oncreate. Do I have to add something like this to bring back the instance
On the second button on the main menu I have added this
Intent intentContiune = new Intent(MainMenu.this, NewClass.class);
intentContiune.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentContiune);
Thanks
Try this:
Home Screen Left Button:
Open the new activity with an intent flag, FLAG_ACTIVITY_NEW_TASK
Activity:
Override onBackClick() on the started activity to call the home screen with an Intent instead of finishing it. Use the flag FLAG_ACTIVITY_REORDER_TO_FRONT
Save activity state overriding OnSaveInstanceState
Home Screen Right Button:
Call the activity with the flag FLAG_ACTIVITY_SINGLE_TOP
More info about flags:
http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/
One solution may involve passing bundles around that include the state of your activity. Using startActivityForResult(), you can return a bundle with the activity's state. When the user clicks your second button, pass in that bundle and have the activity set itself up with respect to the contents of the bundle. If the bundle doesn't contain the information you're looking for, then use the default values as if you were just starting it.
For more information:
Android: Capturing the return of an activity

Categories

Resources