I am calling a activity from my service when an incoming call ends as given below
Intent callIntent1 = new Intent(Intent.ACTION_CALL);
callIntent1.addCategory(Intent.CATEGORY_HOME);
callIntent1.addCategory(Intent.CATEGORY_LAUNCHER);
callIntent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent1.setClass(context, com.example.test.MyActivity.class);
Log.d("TAG", "MyActivity");
startActivity(callIntent1);
but my activity starts for a blink and closes and I see the home screen ,my log shows the call given to MyActivity ,I cannot find the reason for this
I think it is because you are not using your intent categories the right way. If you are explicitly calling the startActivity method, you don't need to add a category to your intent. They are meant to be used with intent filters I guess. If you want to detect something, eg a call, a text etc, add an intent filter to your activity.
Edit: see here
Related
I have 2 activities . Activity 1 has Information. Activity 2 has a form to update Information in Activity 1 .
How can i return yo Activity 1 and show updated information ?
When i move from activity to another should i use finish () ?
Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).
For example, your app can start a camera app and receive the captured photo as a result. Or, you might start the People app in order for the user to select a contact and you'll receive the contact details as a result.
Of course, the activity that responds must be designed to return a result. When it does, it sends the result as another Intent object. Your activity receives it in the onActivityResult() callback.
This the detailed link of about startActivityForResult
Use this pattern.. dont have to override back pressed it will automatically use the super method.. So, in activity 1 use this field var..
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// update the info and UI here
}
};
onResume() of activity 1
LocalBroadcastManager.getInstance(mContext).registerReceiver(mBroadcastReceiver ,new IntentFilter("info"));
and in Activity 2
Intent intent= new Intent("info");
intent.putExtras("infoss",*yourInfo*);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Note The activity 1 will be automatically updated with correct info .. you dont need to use onBackPressed() logic for this
In your first activity
In the second Activity when your work is done and you want to send data from the second activity to the first activity
receive data in the first activity
There 2 apps: First app can open the second one using:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.my.path");
startActivity(intent);
this works fine.
Second app can call again the first app by using intents with actions. for example:
public void call(String number)
{
Intent myIntent = new Intent(Intent.ACTION_CALL);
myIntent.setData(Uri.parse("tel:" + number);
startActivity(myIntent);
}
And there lies a problem which I'll get to shortly.
I'm handling the received intent in the first app in the onCreate method. The first app is simply a single activity with many fragments which are switched with a fragment transaction. When receiving the intent from the second app, I'm making a transaction to a specific fragment according to the intent.
The problem is that when the first app is not in the background (meaning its close), my handling of the intent works fine. However, if the user opened the second app from the first one and the first one is still in the background, then when a user calls the intent in the second app, the first app is getting back to the foreground but to the same fragment the user was when he launched the second app, while I was expecting to show the new fragment which is based on the user's request in the intent sent from the second app.
Why is this happening and how can I fix it?
You could handle this by overriding the OnNewIntent method handling the intent and bringing to top the required fragment.
How do I get my app to appear in on the screen after it has been replaced by some other screen/activity? Some network event occurs and my application wants to reappear in the foreground (presumably in a polite manner).
I think I need to do something like:
contxt.startActivity(myActivity);
I don't want to create another instance of my app or cause it to restart, but I want it to appear.
Use FLAG_ACTIVITY_NEW_TASK
Intent intent = new Intent(contxt, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
contxt.startActivity(intent);
In your myActivity onNewIntent is called. I assume myActivity is the top activity in your app current stack
I have an app for sending sms. It consists of several "cascading" activities - one for writing text, next for choosing number and next for confirming before sending. After sending the message I want that all activities get closed. How can i do this?
You will find no well-written Android apps that have this behavior.
After sending the message, you are welcome to send them back to your main activity using Intent.FLAG_ACTIVITY_CLEAR_TOP, which will remove all intervening activities between the current one and the main activity, using something like:
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If you start all the Activities with startActivityForResult you can chain the finish calls in onActivityResult: basically make the final Activity call finish after setting a specific result code, and each earlier activity checks for that result code and if its there does the same thing. They'll all close up cleanly and you should be all set.
I wanna run two system activities one after another in specific order.
now as we know, startActivity is an asynchronous operation, so i cant keep on a specific order.
so i thought maybe I should try to do it with dialogBox in the middle but also running a dialogBox is an asynchronous.
now as i said the activities which i try to run are system activities, so i cant even start them with startActivityForResult (or mybe i can, but i cant think how it will help me).
Any tricks how could i manage with this issue?
Some code:
first activity:
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
startActivity(intent);
second activity:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(tmpPackageFile
.getAbsoluteFile()),
"application/vnd.android.package-archive");
startActivity(intent);
as you can see, i dont have any access to those activites, i can just run thire intents from an outside class/activity/service.
You should be able to use startActivityForResult.. The second parameter to that function is a unique id, which you can use to track which activity is ending.
In onActivityResult of the calling activity, check which activity just finished, then start the next one with another call to startActivityForResult (or, if you don't care what happens with the 2nd, just startActivity).
I may be missing the boat on this, it seems you should place your code to start the second activity in the handler that finishes the first activity, such as on a button press or when an item is selected from a ListView. More information on how the first Activity is terminated would help.