Theme affecting code function? - android

Is there some reason applying a theme to an activity would affect the code function? I was under the impression that styles/themes merely affected appearance...
I have a listfragment (filled from a database) with a button at the bottom, upon the press of that button a new activity is launched to allow you to edit or add to the list/database.
Intent i = new Intent(getActivity(), Activity2.class);
startActivityForResult(i, ACTIVITY_EDIT);
This second activity works as it should and upon exiting back to the listfragment, said list is updated and the new item appears in the list.
Here's where I start having an issue...
Since the second activity is only a TextView, EditText and two buttons, I thought I would use the dialog theme to keep it from taking up the entire tablet screen unnecessarily. From the manifest file:
<activity android:name="Activity2" android:theme="#android:style/Theme.Dialog" />
This does achieve the result I was looking for as far as appearance of the activity goes, but upon exiting activity #2, the list in the listfragment is not regenerated/redisplayed. The additon to the DB is being done, as I can see when I re-start the app the item I added previously finally appears.
The above addition to the manifest is the ONLY change made.
Any thoughts on why this is happening and how to stop it?

Or you could add code in onResume() to refresh it. When it calls the dialog, it calls onPause() and then calls onResume() when the dialog is exited.

Your first activity isn't stopped and restarted when you launch a dialog, but it is when you launch a full screen activity. Try calling notifyDataSetChanged on the list adapter after the dialog activity finishes.

Related

how to know if Activity resumed by shortcut or by another top activity

The main activity creates a shortcut to homescreen, all goes good but it needs to know an extra value when it starts by shortcut. So I used this while creating shortcut
intent.putExtra(EXTRA_ID, "Some string info here");
And onResume retrieve value like this
if (getIntent().hasExtra(EXTRA_ID))
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
Everything works fine, lets take some examples
Activity never launched, Home screen shortcut pressed and A toast appears, Nice.
Activity is in background, Home screen shortcut pressed and A toast appears, Nice.
Activity is in use, another activity is launched, when back, toast appears, Not nice.
Activity is in background with no extra, Shortcut pressed, Nothing appears.
In 3rd example, it does what it suppose to do. But how do I know if activity is resumed/launched by shortcut ?
You set an extra in launcher Intent, and want to get the extra when this activity is opened in any state.
The problem you meet:
Activity is in background with no extra, Shortcut pressed, Nothing
appears.
This is because that your activity is started already, the launcher intent just use the existing task as the front task, and its extra data is lost in such situation.
To solve this, you need 2 steps:
Add android:launchMode="singleTask" to this activity in your manifest.
Put the extra data handling codes in both onCreate() and onNewIntent(), not in onResume().
How are you removing the extra EXTRA_ID? You should do it like:
In onResume():
if (getIntent().hasExtra(EXTRA_ID)) {
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
getIntent().removeExtra(EXTRA_ID);
}

Dialog Activity also resumes other activities in the background

This is the scenario. I have 2 activities in my application and a Dialog activity that is started when I click on the notification created by my application.
The problem is that when I click on the notification, only the Dialog should show, not the other activity of my app, if it was stopped on pressing the home button.
When I close my application by pressing the back button, the dialog activity shows the dialog, but when the application is running in the background, that activity also opens up on creating the dialog activity.
I use #android:style/Theme.Dialog for my dialog activity.
How to only show the Dialog activity, not other activities in the backgroud?
The solution to the OP issue is setting a different Task affinity for that activity in the Manifest.
<activity android:name="MyIndependentActivity"
android:theme="#android:style/Theme.Dialog"
android:taskAffinity="a_unique_id">
The taskAffinity string must be different from the package name (com.whatever.myapp)
I also use android:excludeFromRecents="true", to hide the dialog from the recent apps list, as it usually makes no sense returning to a dialog.
More info: http://developer.android.com/guide/components/tasks-and-back-stack.html#Affinities
Yeah that'll happen.
When you declare a theme of Dialog it affects the activity lifecycle and the previous activity doesn't go into onStop so some Android functions still think it's the active activity which is technially true as your dialog Activity is acting like a dialog.
One possible work around if you don't 'care' that you can see the previous activity behind the dialog is to change the dialog to be a DialogFragment, put the theme of dialog on the fragment and show this in it's own activity, that'll do it.
The way I have done this (and this can't be the best way to do this) is to have some logic in the activity.onPause() and activity.onResume() methods, that will perform actions based on what I want. My experience is more around separate activities and transitioning between them than using dialogs alot.
You can pass information between activities through setResult(). This will enable you to work out why the child activity has decided to close. That combined with the onResume function should enable you to disable the parent activity.
To override the dialog so that the other activity is not visible behind it, is probably to use the onPause() method to make it go translucent.
I have found onStop() very irratic to use and often unnecessary. The reason for this, is that it is called unpredictably from a developers point of view because onStop can be called based at strange times based on whether the OS has enough memory etc. onPause however in my experience is always called predicably.

Advice related to back button on the android device

Hey guys I am new to android development, I am currently making an application.The main activity has a list view and all the items are getting displayed in the list view, as they should.Now if any item is clicked or if a new item is added, a new activity is opened up and gets destroyed if the back button is pressed.Now when the main activity is reached and the back button is again pressed to close the application , it instead of closing the application , moves again to the main activity,only this time, the last entry added is not present, similarly on continuously pressing the back button all the entries first get removed, and when all the entries get removed, the main activity closes and the application stops. Any idea why this is happening, I could really use your help. Thanks.
You need to add android:noHistory="true" to your list detail Activity in the AndroidManifest.xml
For example:
<activity android:name=".MainList"/>
<activity android:name=".ListDetail" android:noHistory="true"/>
Alternatively, you could override the onBackPressed method to finish the activity when you press the button.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

Avoiding Blank Screen when it Raised from Intent in Android

I have an application that raises from service on scheduled time. In that service, i have displayed my Activity using Intent. It works perfectly. But, when i go back, the activity is finished. But, one blank screen is remaining there? How can i avoid this blank screen. I need my application's Activity instead of blank screen. Anyone Guide me.
#Špĸ remove this line: i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
take a look at my post if you wish to open a dialog via your service: Creating global dialogs
I don't like that you finish an activity that you go back to. You have multiple of different alternatives:
Using fragments instead of activity intents.
Set flag to indicate the activity is done so it will close when you go back.
You could also try to work with the stack, look at this: https://stackoverflow.com/a/4038637/969325
Without seeing the code it is very diffcult to find out what is going wrong in your code but still I am making some effort to help you.
Add android:theme="#android:style/Theme.Translucent" to your activity tag in your manifest file and then try if it works or not.
Have you tried setting FLAG_ACTIVITY_NO_HISTORY in the intent you use to start the AlertDialog activity or launchMode="noHistory" in the manifest.
The AlertDialog activity may very well be the blank screen you're seeing as no content view is set and the only thing a user sees is an alert dialog so once the alert dialog is dismissed and the app goes back to this screen it would be blank.
Before press back button, you have to save the state of the activity and restore the activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.

Opening dialog activity on creating another activity

I've got an activity, call it A, that when initially opened I would like to programmatically open a another dialog theme activity, B, over the top. The user would select some info from the dialog activity and return to activity A. When resuming to activity A at some later point the data set would be available, so the dialog would not need to be shown.
The problem is attempting to start the dialog activity from activity A's onResume or onCreate methods causes strange and undesirable behaviour. The task appears to kind of freezes for a good few seconds and then the dialog is displayed correctly, but activity A isn't shown behind.
Thanks
Peter
check this:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/CustomDialogActivity.html
i think u should create activity as a dialog.then it helps
u can set style and theme for ur activity by this..

Categories

Resources