my app is opened instead of the specific activity that I call - android

I am trying to show a dialog when picture taken so I setup a broadcast receiver on picture taken by default camera app.
I read that I need to call an activity to achieve that. I setup the activity so the theme in the manifest is like transparent
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen
here is my code
public class CameraEventReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Cursor cursor = context.getContentResolver().query(intent.getData(), null,null, null, null);
cursor.moveToFirst();
String image_path = cursor.getString(cursor.getColumnIndex("_data"));
Toast.makeText(context, "New Photo is Saved as : -" + image_path, Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, DialogActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
The problem is that my main app activity is being displayed behind the Dialog activity so that the main activity is shown. I just want the dialog activity to be displayed on top of the picture.
Is there a way to fix that?
Thank you
EDIT: As requested, my manifest:
<application
android:allowBackup="true"
android:name="MyApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.xx.xx.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DialogAcitivity" android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.xx.xx.DIALOGACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver
android:name=".CameraEventReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE" />
<action android:name="android.hardware.action.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>
</application>
My code for button, basically ask the user for a value:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
promptUser();
}
private void promptUser() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setMessage(R.string.msg)
.setCancelable(false)
.setView(input)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
storeValue(value);
dialog.dismiss();
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}

the reason your app's activity shows behind the dialog is because android's switching the entire activity task. your dialog activity was (i'm assuming) started in the same task as your other activities.
what you want is to give your dialog activity a different task affinity than the (default) affinity of your other activities.
<activity
...
android:taskAffinity="com.foo.bar.myaffinity"/>
now this activity won't be launched into the same task as the rest of your activities, and when you start it, those other activities won't show behind it.

Check this post
What you want is to get your image from the result, then display it in a layout in some activity. Once the image is displayed, launch your dialog.

Related

How to correctly handle custom URL scheme in my app (avoid handling multiple times)?

I have added a custom URL scheme to my Android 4+ app to be able to deep link to some view/activity:
// AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="myapp"/>
<data android:host="webservice"/>
</intent-filter>
</activity>
// MainActivity
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
if (uri.getScheme().equalsIgnoreCase("myapp") && uri.getHost().equalsIgnoreCase("webservice")) {
Intent settingsIntent = new Intent(MainActivity.this, WebServiceSettingsActivity.class);
startActivity(settingsIntent);
}
}
}
This works fine, when a link like myapp://webservice/something is used, my app is startet and the WebServiceSettingsActivity is shown.
BUT: When pressing the back button, the WebServiceSettingsActivity is shown again. It seem, that MainActivity is re-startet with the same intent as before and thus the settings are shown again...
This can be repeated indefinitely. How to solve this?
Your MainActivity is opening your WebServiceSettingsActivity, but you're never finishing the former. This leaves you with 2 choices:
Call this.finish(); immediately after calling startActivity(settingsIntent);
This will kill your MainActivity so that there is nothing to return to after finishing the WebServiceSettingsActivity.
2 (better). Open your WebServiceSettingsActivity with startActivityForResult() instead of startActivity(), and finish your MainActivity when, and only when, returning from your WebServiceSettingsActivity:
private static final int REQUEST_CODE_WEB = 1;
public void onCreate(Bundle savedInstance) {
...
if (...) {
startActivityForResult(settingsIntent, REQUEST_CODE_WEB);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_WEB) {
finish(); // Or do whatever you want, such as load your main content
}
}

getApplicationContext() and intent filters

I'm making a pdf file viewer app with this library. I applied other things successfully, but I'm continuously failing to link the app with "Complete action using" list of Android.
First, init() method of the library is as follows(MainActivity.java):
private void init() {
lv_pdf = (ListView)findViewById(R.id.lv_pdf);
dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
fn_permission();
lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), PdfActivity.class);
intent.putExtra("position", i);
startActivity(intent);
Log.e("Position", i + "");
}
});
}
Second, AndroidManifest.xml file is as follows:
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PdfActivity"
android:screenOrientation="portrait">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
MainActivity.java defines permission and start screens of the app, and PdfActivity.java shows content of the pdf files.
The Problem:
init() method have a implicit intent with getApplicationContext(). How to I link this to intent filter in AndroidManifest.xml file? I want to launch the app through "Complete action using" screens.
Too can you use it
Intent intent = new (this, PdfActivity.class);
Instead of getApplicationContext() use MainActivity.this

Implicit vs Explicit Intent in Android ( startActivity(intent) crashes )

I'm new in this world. I have a problem when I use startActivity(intent).
This is the Manifest:
<activity
android:name="com.example.counter.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.counter.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And this is the code:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent i=new Intent ("com.example.counter.MainActivity");
startActivity(i);
}
}
};
timer.start();
}
I'd want to show Splash activity for 5 seconds and then show MainActivity.
LogErrors: !https://www.dropbox.com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png
There are two ways of doing what you are trying to do.
Using an implicit Intent
Using an explicit Intent
Refer Intent Types
Implicit Intent
Declare Intent Filters for your Activity in your AndroidManifest.xml. By doing that the Android system understands what kind of Intents your component(in this case your MainActivity) can handle.
<activity
android:name="com.example.counter.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.counter.MainAction" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity>
Now you will be able to launch your Activity with the same Intent
Intent i=new Intent ("com.example.counter.MainAction");
startActivity(i);
Such implicit Intents are used when you don't explicitly know which Activity has to be started and you want the Android system to decide which component to start. If the system finds multiple components which can handle your Intent, it will allow the user to choose.
Note: it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object.
Explicit Intent
In your case, you should use an explicit Intent as you already know which Activity you want to start. So create an Intent by passing the context and the component(Activity) class you want to start.
Intent i=new Intent (this,MainActivity.class);
startActivity(i);
You have to reference the class you want to start. So you'd need something like:
Intent newAct = new Intent(this, Splash.class);
startActivity(newAct);
What you're passing is an Action that is not understood as a class name.
I guess, Splash is your Launcher Activity, make following changes in your manifest file:
<activity
android:name="com.example.counter.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.counter.MainActivity"
android:label="#string/app_name" >
</activity>
Make your activity this way:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/*Splach screen that display for 5 seconds when app start*/
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
finish();
}
}, 5000);
}
}
I hope this should solve your problem now.
I think you should be able to use (implicit Intent):
Intent i=new Intent ("com.example.counter.MainActivity");
There is no reason to change it to (explicit intent):
startActivity(new Intent(mContext, MainActivity.class));
but then you need to change the action in intent filterof MainActivity from:
<action android:name="android.intent.action.MAIN" />
to:
<action android:name="com.example.counter.MainActivity"/>
You need to declare an activity in manifest file.
Like this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activty
android:name="com.example.counter.MainActivity"/>
</application>
Hope it helps.
i think it is better if you use Handler put this code at the splash Activity at the onCreate
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}, 1500);
and if you want to open it one time it is good to use SharedPreferences

How to force app that has an main activity with category home set to close

i'm trying to develope an app for a tablet (ICS 4.0.3) that will be used in public places like bar, resturant ecc..
The user that uses that tablet ( so my application ) could not go in home and only administrator, setting a code, can go out.
What i've done is:
public class MainActivity extends Activity {
private Activity actual;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (getIntent().getBooleanExtra("EXIT", false)) {
finish(); // it doesn't work
}
}
#Override
public void onBackPressed() {
// do nothing!
}
}
MANIFEST:
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>
ADMINISTRATION DIALOG:
TEST1: // application is relaunched
Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIntent);
TEST2: // application is relaunched
System.exit(0);
TEST3: // it open settings (o.O)
Intent h = new Intent(Intent.ACTION_MAIN);
h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
h.putExtra("EXIT", true);
context.startActivity(h);
My problem is that i can't go out from application.. how can i solve?
In TEST3 you've only provided ACTION_MAIN in the Intent. Android looks for apps that can handle this action and finds a long list of them. How should it know that it should launch yours?
I assume you've set your app as a HOME-screen replacement. Try adding this to the code for TEST3:
h.addCategory(Intent.CATEGORY_HOME);

android open dialogue activity without opening main activity behind it

Im writing a program that offers a quick reply dialog upon receipt of an SMS.
However, I am getting an unexpected result. When I receieve an SMS, the appropriate dialog activity comes up displaying the correct phone number and message, however there is a second activity behind it that is the 'default' activity in my program (it is what opens when i launch my application)
I do not want this second activity to come up. The quick reply activity should come up by itself over top of whatever the user was doing before.
The 'floating' activity:
public class quickReply extends Activity {
String mNumber, mMessage;
TextView mMainText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (TextView)findViewById(R.id.mainText);
try{
Intent i = getIntent();
Bundle extras = i.getExtras();
mNumber = extras.getString("theNumber");
mMessage = extras.getString("theMessage");
this.setTitle("Message From:" + mNumber);
mMainText.setText(mMessage);
} catch(Exception e) {
mMainText.setText(e.getMessage());
}
}
}
The call to the activity inside an onReceive()
Intent i = new Intent(context, quickReply.class);
i.putExtra("theNumber", mNumber);
i.putExtra("theMessage", mMessage);
i.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
The Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
the only way I have found that works, in your activity definition in manifest:
android:launchMode="singleInstance"
but then you have to relaunch your main/default activity once the dialog is dismissed. NOTE: you will lose all state from the previous launch, so this is a less than ideal solution.
UPDATE:
you can also do this by:
Intent.FLAG_ACTIVITY_CLEAR_TASK
so here's what I did:
open the original/main activity
from a service, launch the dialog style activity using the above (main goes bye-bye).
when the user dismisses the dialog, start main again with an extra intent (IS_BACK) that is processed in onCreate() and calls:
moveTaskToBack(true);
this will keep the task under the dialog on top and your main in the back of the stack.
You should set the task affinity of the activity to something different than your main activity. This will separate it from the main activity and it will track as a separate task:
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:launchMode="singleTask"
android:taskAffinity="quickReply"
>

Categories

Resources