Navigate back to app after closing share dialog - android

I have a share button in my application, whose purpose is to share a string of information with user selected app (for example twitter or gmail). The problem is when you click the share button, the share window appears and the app becomes black in the background. After done sharing, the user is returned to menu / home screen and has to open the app again in order to continue using it from where they left.
What I need is to go back to my app after done sharing.
This is the OnClickListener I used:
shareButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = mContext.getString(R.string.shareText) + " " + profileInfo.getName() + " " + mContext.getString(R.string.shareText2);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.setType("text/plain");
startActivity(shareIntent);
}
}
);
What am I doing wrong here? Any help is appreciated.

Use startActivityForResult() instead of startActivity(). This will return to the starting Activity after the Intent action is completed. See the Getting a Result from an Activity post for an example.
And wait for response by overriding onActivityResult() method :
#Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub if(requestCode == 0) {
// You will get callback here when email activity is exited
// perform you task here
}

There is noting wrong with your share intent code which you mentioned above, although there could be some thing wrong with your onPause() method, you probably doing some thing there, try to debug the code after adding logs So you can track the issue OR put your activity code here So exact issue can be identified.

Related

Android Facebook Login Integration - Issue in Profile Picture

UPDATE
Question 1 Solved
I have solved the issue of profile pic not loading by changing the context of Glide from getApplicationContext to HomeActivity.this and image loaded quickly. :)
But still, I need your help with other two questions. If I am doing it right or wrong and how to avoid that error.
Question 1
So far, I have successfully connected my app for facebook login, but the issue is the profile picture.
What I have done is created 2 activity:-
1. login button of Facebook in Activity 1
2. Home Activity of App.
If the user is logged in, it goes to Activity 2 and if not he goes to activity 1.
On activity 1, I have fetched basic details like dob and email address as name and profile picture doesn't need to be fetched using GraphRequest. (To be able to save on my server)
On activity 2, I have fetched the user name and profile picture. Here's the code:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
im = (ImageView) findViewById(R.id.img);
name = (EditText) findViewById(R.id.name);
try {
String url = Profile.getCurrentProfile().getProfilePictureUri(150,150).toString();
name.setText(" "+Profile.getCurrentProfile().getName()+"\n"+Profile.getCurrentProfile().getId());
Glide.with(getApplicationContext()).load(url).into(im);
} catch (Exception e){
Toast.makeText(this, "Error : "+e , Toast.LENGTH_SHORT).show();
}
}
Until 2 days back, profile pic was loading fine but from today it's not loading at all.
Question 2
Another question, I have is that sometimes I get an error message when I log in and get in Home Activity, here's the snapshot of the same:-
Question 3
The last question is how to close the entire app on back pressed from activity 2.
What I have done right now is use handler on Activity 1 so that the above error doesn't come and then use Intent and below that use finish() method on activity 1, and on activity 2:-
public void onBackPressed() {
onStop();
onDestroy();
finish();
}
To Question 3
from Activity 1 create a function onActivityResult
and be sure to use the startActivityForResult to call the Activity 2 instead of startActivity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivityForResult(new Intent(this, ActivityTwo.class), 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (data.getBooleanExtra("EXIT", false)) {
finish();
}
}
}
and to close the entire app from Activity 2 using the onBackPressed.
don't forget to remove the super.onBackPressed()
#Override
public void onBackPressed() {
// super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("EXIT", true);
setResult(RESULT_OK, intent);
finish();
}

Android - Trying to get reference to a locally stored image with intent results in unexpected behavior

What should be happening:
A user clicks on a button which brings up their phone's image gallery. They select an image via startActivityForResult, which I then get the path for and handle it from there.
What is happening:
When I click the button, the gallery opens up perfectly. But when I select an image, onActivityResult is never called and instead of closing the gallery and going back to the initial activity, the app goes back to the activity before it. Like it's calling onBackPressed or something.
My code for opening the gallery:
profilePicView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
}
Trying to get the result (this never gets called):
public void onActivityResult(int requestCode, int resultCode, Intent data){
// none of this is every called, including the entire onActivityResult method
if(requestCode == 1){
Uri selectedImageUri = data.getData();
selectedProfilePicPath = getPath(selectedImageUri);
}
}
So all I'm trying to do is get the path to the selected image with onActivityResult. But that is never being called and for some reason this action causes the activity to go back to the previous activity. If it matters, this is an Activity and not a Fragment.
Thanks
The issue was that I had "noHistory=true" defined in the Android Manifest for this Activity. Removing that fixed everything.

How to know when Activity is finished in android?

In my android app I want to change the input method. So I start a new Activity which shows the language settings in the device. Then user can change it. However then I want to know that if the user has changed it. So I wrote a function for that also. My code so far is...
Intent enableIME = new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
startActivityForResult(enableIME,0);
if(isInputMethodEnabled()){
activateshadow.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
activateshadow.setText("Deactivate Shadow");
prefs.edit().putBoolean("Activate", false).commit();
}else{
Toast.makeText(MainActivity.this,"You haven't change the input method to simpleIME.In order to activate you must change it.",Toast.LENGTH_SHORT).show();
}
my is inputMethodEnabled function is....
public boolean isInputMethodEnabled() {
boolean isIME ;
String id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
String [] name = id.split("/.");
// Toast.makeText(MainActivity.this,"s:"+name[1]+":s",Toast.LENGTH_SHORT).show();
if(name[1].contains("SimpleIME") ){
isIME = true ;
}else{
isIME = false;
}
// Toast.makeText(MainActivity.this,"Returning..."+isIME,Toast.LENGTH_SHORT).show();
return isIME;
}
if(isInputMethodEnabled()) always fails because when the new intent(settings) opens and it take some time to change the input method to simpleIME . How to fix this problem?
You catch when a launched Activity returns in onActivityResult. The requestCode you supplied to startActivityForResult will be a parameter, as will the Activity's result. The Activity may also set other data which you didn't ask about.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 555) {//555 is the intent ID you gave in startActivityForResult(enableIME,555);
if (resultCode == /*Result1*/)
//Do something
else {
//Do something else
}
}
}
You need a unique id when calling startActivityForResult(enableIME,0);
startActivityForResult(enableIME, 555);
Better still replace 555 with a named variable.
if u look at android life cycle, when activity is finished whole android call onDestroy() method.
so u can call and override this method.
just need write:
#override
protected void onDestroy(){
// code
super.onDestroy();
}
u can manage and override all of life cycle's parts in android
e.g: onResume to get current activity
i hope this help u

onactivityforresult not called after contactscontract action_edit

I have a ListActivity which displays all the aggregate contacts. When the user clicks one, my code calls startActivityForResult. All this works properly. When the user finishes editing, I want my ListActivity to be displayed again. Instead, the "people" activity gets displayed. Similarly, my onActivityResult function never gets called.
Here is the code handling the click:
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
Cursor cur = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
cur.moveToPosition (position);
String key = cur.getString (2); // "2" is the col containing the LOOKUP_KEY
System.out.println ("clicked " + key);
// make intent to edit contact
Intent intent = new Intent (Intent.ACTION_EDIT);
intent.setData (Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + key));
startActivityForResult (intent, 2);
}
And I also have an onActivityResult function:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
System.out.println ("request " + requestCode + ", result " + resultCode);
}
Any suggestions?
I filed a bug to android about this. Someone looked at it and responded that there is an undocumented workaround. From the bug report:
The undocumented workaround is to call putExtra("finishActivityOnSaveCompleted", true); on the ACTION_EDIT Intent.
However, as this is undocumented, I have no idea which Android version(s) will use it.
I tried it and it works for the version of Android I'm using: 4.1.2. See issue 39262 for more info.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 2) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
Replace your onActivity result to this. for request code get after
they will work
Add
super.onActivityResult(requestCode, resultCode, data);
in OnActivtyResult().
Make sure you are calling startActivityForResult from an activity, then only your onActivityResult will be called. For example, if you have the similar code in an fragment, the onActivityResult will never be called.

Extending Android's Voice Search app

Is it possible to extend the Voice Search app? I know I can add a button in my own app to launch the voice recognition dialog, but I was wondering if I could extend the voice search app that launches automatically when you long press the physical "search" key.
send text to [contact] [message]
listen to [artist/song/album]
call [business]
call [contact]
send email to [contact] [message]
go to [website]
note to self [note]
navigate to [location/business name]
directions to [location/business name]
map of [location]
I'd basically like to add my own action to the above list.
Is this possible or will I have to create my own?
In a word, no. The app doesn't have the extension points you are looking for. You would have to write your own app entirely, which is something that others have done.
A simple method to Handle Voice Search
Step 1 Call this method on button click
public void startVoiceRecognition() {
Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
intent.putExtra("android.speech.extra.LANGUAGE_MODEL", "free_form");
intent.putExtra("android.speech.extra.PROMPT", "Speak Now");
this.mContainerActivity.startActivityForResult(intent, 3012);
}
Step 2 Override onActivityResult method
# Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 3012 && resultCode == RESULT_OK) {
ArrayList < String > matches = data.getStringArrayListExtra("android.speech.extra.RESULTS");
String result= matches.get(0);
//Consume result
edittext.setText(result);
}
}
Thats all, DONE

Categories

Resources