Android how to open Activity when open home screen widget? - android

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.

Related

Close PreferenceFragment after widget configuration complete

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();

How to correctly define activities and intents to behave as desired?

I have a 'list' activity which starts an 'article' activity when clicked.
I also have push notifications which opens the 'article' activity directly.
I changed the back button behavior in the 'article' activity to start the 'list' activity, when coming from a notification so that the user will go back to the article list.
The problem is when the app is already opened in the background and I open a notification - it just brings it back to front.
What I want to achieve is open the right article when clicking a notification and going back to the 'list' activity, without having the possibility the the list activity will be open twice.
I tried to separate the 'article' task and create new task in the notification intent but then it would open separate 'list' activities when opening multiple notifications and clicking back.
What is the correct way to define the activities' tasks and intent flags to achieve my goal?
EDIT:
Manifest part:
<activity android:name="ListFeed" android:configChanges="orientation|screenLayout" android:launchMode="singleInstance" android:screenOrientation="unspecified"
android:taskAffinity="com.app.MyTask"></activity>
<activity android:name="Article" android:launchMode="standard" android:configChanges="orientation|screenLayout" android:screenOrientation="unspecified"
android:taskAffinity="com.app.MyTask"></activity>
Notification intent:
Intent notificationIntent = new Intent(context, Article.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, notificationID, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Thanks!!
what i got from your question is that
1) you have listActivity A
2) ArticalActivity B.
i) And first you want to open Activity A whenever back from B, Correct? for that you can use dispatchKeyEvent, listen to Back button event and start activity A. or by using below code
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
ii) you want to open only single instance of Activity A(list). for this you can basically use
launchMode in Activity A's Manifest declration as singleInstance.
android:launchMode="singleTask"
you can read docs for launch mode
let me know if i missed anything.
I see that you are playing around with launchModes and excludeFromRecents and this isn't a good thing. The standard behaviour of Android should do pretty much what you want.
To verify this I've created a simple 3-activity application that contains a MainActivity, a ListActivity and an ArticleActivity. I'm not using any non-standard launch modes and I'm not setting any Intent flags (except in onBackPressed() see below). The Main Activity creates and posts a notification to display a specific Article. The MainActivity starts the ListActivity. Each element of the ListActivity starts an Intent for the ArticleActivity and passes some information in EXTRAS so that the ArticleActivity knows which article to display.
In order to have the behaviour you described (ie: returning from the ArticleActivity to the ListActivity after starting the app from a notification, even if the app was not running), I've done what Ankit has suggested (ie: override onBackPressed() in ArticleActivity) like this:
#Override
public void onBackPressed() {
// Return to ListActivity
Intent intent = new Intent(this, ListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
// Finish this activity (in case the ListActivity wasn't already in the stack)
finish();
}
I used FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP because this will not recreate the ListActivity if it already exists in the activity stack (ie: it will go back to the same instance).
I had to add the finish() call, because if the app was not running in the background and the user started it from the notification, the ListActivity would be created and put on top of the ArticleActivity. Then when the user pressed "back" to leave the ListActivity, the ArticleActivity would be exposed underneath. Adding finish() here makes the ArticleActivity go away so that pressing "back" from the ListActivity goes back to wherever it came from.
If you want me to send you the code, just let me know.

Launch an Activity on the first launch of a widget

I would like to launch an Activity, when the user adds the widget on the launcher.
How can I do that ?
The onReceive method is called too often.
And with onEnabled, it simply doesn't launch.
How can I do that ?
Tkx
I'm not sure on this, I havne't done a widget yet, but I think when you create a widget, the widgets onCreate() method gets called. Try placing your startActivity(Intent) in there and see if that works.
Widget Doesn't have a OnCreate() method. Instead it has a onEnabled() Method.
#Override
public void onEnabled (Context context){
super.onEnabled(context);
Toast.makeText(context, "Launching Config Activity", Toast.LENGTH_SHORT).show();
//Launching the Widget Config Activity on creating widget first time
myIntent = new Intent(context, ConfigActivity.class);
//Needed because activity is launched from outside another activity
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtra("WIDGET_SIZE", "default");
context.startActivity(myIntent);
}
Remember you need to add the widget to the home screen using code after the configuration is complete with necessary changes.
Refer more here:
http://developer.android.com/guide/topics/appwidgets/index.html

Activity not being reused

I have an odd issue with my application.
I start the application and the activity shows ok.
I then press the home key so that the activity goes into the background. I can see the onPause() method being called.
The application's service then creates a notification which shows on the status bar.
I then click on the notification and the activity is shown and I see that the onResume() method is called.
I then press the home key and the activity goes into the background. I can see the onPause() method being called.
If I now start the application by clicking on the applications icon I see that a new instance of the activity is created rather than using the paused instance.
If I press the home key again the new activity goes into the background.
Starting the application by clicking on the applications icon I see another new instance of the activity is created.
Pressing the back button at the point destroys each activity in return.
What I want to happen is that a single instance of the activity be used.
Any ideas?
Just use the same intent filters as android uses when launches the app:
final Intent notificationIntent = new Intent(context, MessageListActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.
You should look at the different launch modes for Android activities. this should help. Launch modes can be set in the androidmanifest.xml file. I think your solution would be to use the 'singleTop' launch mode.

How to go to previous activity if the activity is killed by Android runtime?

I had a problem with an app, i.e., when I am moving to the previous window the control goes to the splash screen of my app because the previous activity is killed by the Android runtime.
How do I keep the previous activity alive, or how can I create the activity again and access it from the present activity.
You can make use of a default constructor to close and open the screen.
If you want to move between screens:
1-2-3-4
&&
4-3-2-1
Android itself will take care of it.
For example, if you want traverse in a specific way like
4-2-3-1
Android doesn't allow you, so simply create a constructor and assign present activity to it and make use of that variable to close the screen at any time.
For example, here is the activity class
public class One extends Activity
{
private One Screen;
public One()
{
Screen=this;
}
}
When you want close this activity simply use:
Screen.finish();
instead of
this.finish();
When you want invoke new activity of your liking, simply use
Screen.finish();
Intent i = new Intent(One.this, YourActivity.class);
Log.i(TAG, "calling YourActivity Screen");
startActivity(i);
If you want pass any data between the screens, you can make use of
i.putExtra("valuename", actualvalue);
and you can retrive the value using
Intent startIntent = getIntent();
String actualvalue = startIntent.getStringExtra("valuename");
When you have a Splash activity, I think the correct way is closing in and starting the main activity
Intent mainIntent =new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
Then, from your main activity start calling your subactivities.
Why has it killed your main activity? Are you calling finish()?

Categories

Resources