I'm trying to take a picture with a Google Glass App. Therefore I'm using a SurfaceView which shows the camera preview in the background. The photo is taken with an intent.
The problem is that the onActivityResult method belonging to the intent is never called. I've read that this problem is a bug on Google Glass but should be closed with the newer versions of Google Glass.
onCreate Method:
private CameraSurfaceView cameraView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initiate CameraView
cameraView = new CameraSurfaceView(this);
// Set the view
this.setContentView(cameraView);
}
Intent Calling:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_camera:
take picture
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent != null)
{
Toast.makeText(getApplicationContext(), "Taking Picture",
Toast.LENGTH_SHORT).show();
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This all works fine. The user sees the camera preview and when the intent is called a picture will be taken and stored. After that the user sees a prompt "Tap to Accept". After tapping the app ends but the onActivityResult method is never called.
onActivityResult Method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i("Camera", "Hello from onActivityResult");
// Handle photos
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK)
{
String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
}
super.onActivityResult(requestCode, resultCode, data);
}
Releasing the camera:
#Override
protected void onResume() {
super.onResume();
// Do not hold the camera during onResume
if (cameraView != null) {
cameraView.releaseCamera();
}
}
#Override
protected void onPause() {
super.onPause();
// Do not hold the camera during onPause
if (cameraView != null) {
cameraView.releaseCamera();
}
}
Logcat Log for my application with Log Level "Info"
05-03 08:45:02.328 29785-29785/com.dhbw.charadect I/dalvikvm-heap﹕ Grow heap (frag case) to 5.955MB for 921616-byte allocation
05-03 08:45:03.305 29785-29785/com.dhbw.charadect I/Choreographer﹕ Skipped 51 frames! The application may be doing too much work on its main thread.
05-03 08:45:03.313 29785-29785/com.dhbw.charadect W/Resources﹕ Converting to boolean: TypedValue{t=0x3/d=0x210 "res/anim/decelerate_interpolator.xml" a=1 r=0x10a0006}
05-03 08:45:04.008 29785-29785/com.dhbw.charadect I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
05-03 08:45:14.414 29785-29797/com.dhbw.charadect I/Camera﹕ Received CAMERA_MSG_RELEASE
Thanks in advance for all answers and comments!
Related
"Ex: In flipkart when we open App, Home screen will load the data and when we go to next activity from home screen and again we come back to Home screen its not loading again "
Cancel all the tasks using lifecycle methods of Fragment: "onPause", "onStop" and etc (you have to choose one of them). Fragment lifecycle.
Use onActivityResult
Define constant
public static final int REQUEST_CODE = 1;
Call your custom dialog activity using intent
Intent intent = new Intent(Activity.this,
CustomDialogActivity.class);
startActivityForResult(intent , REQUEST_CODE);
Now use onActivityResult to retrieve the result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//reload data here
}
} catch (Exception ex) {
Toast.makeText(Activity.this, ex.toString(),
Toast.LENGTH_SHORT).show();
}
}
In custom dialog activity use this code to set result
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();
While tap on bottom back button it will not reload, because it just finishes current activity and resumes home activity.
To prevent reload home activity while tap on back button from toolbar, you have to add
android:launchMode="singleTop"
to your home activity in your manifest file.
For more details refer here
I have an Activity which makes use of KeyguardManager.
The intention is to disallow the user to use the app, if they are unable to successfully supply their credentials.
Though the keyguard intent appears at the start of the app, pressing the device back button moves the intent away, showing the activity which started it.
Overriding the onBackPressed does not seem to help, as it isn't associated with the intent.
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (km.isKeyguardSecure()) {
setShowWhenLocked(true);
Intent i = km.createConfirmDeviceCredentialIntent("Authentication required", "password");
startActivityForResult(i, CODE_AUTHENTICATION_VERIFICATION);
}
}
What if you use finish() after startActivity() ?
EDIT:
Add finish() on your onActivityResult() if the pattern is false.
What you want to achieve can be done using a "Staging" Activity. For example, you can have a LoginActivity that will check if the user is authenticate or not then from there decide where to redirect him.
The LoginActivity should look something like this, of course you need to adapt it to your business logic :
public class LoginActivity extends AppCompatActivity {
private static final int CODE_AUTHENTICATION_VERIFICATION = 24;
private boolean isFirstLaunch = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
isFirstLaunch = false;
//startActivityForResult With your intent to authenticate the user
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CODE_AUTHENTICATION_VERIFICATION){
Log.i("LOGIN", "return from key guard");
//Check the data and decide if you redirect the user to main activity or not
}
}
#Override
protected void onResume() {
super.onResume();
if(!isFirstLaunch){
Log.i("LOGIN", "resume not first launch");
// the user tried to cancel the authentication either present him with the authentication process again or finish() the activity
}
}
}
Please, N/B: Overriding the onBackPressed does help only when you create a conditional statement controlled by a boolean variable in the onBackPressed method and call it in the onActivityResult i.e when the resultCode != RESULT_OK. Another option is to exit the app when resultCode != RESULT_OK (moveTaskToBack(true)) Here is what I mean below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INTENT_AUTHENTICATE) {
if (resultCode == RESULT_OK) {
//do something you want when pass the security
} else //resultCode != RESULT_OK
//Option 1 Ensure you override onBackPressed() with a conditional
//statement controlled by a boolean variable.
onBackPressed();
//Option 2
moveTaskToBack(true); //Exit app when a user click the back button.
}
}
So Im using the ArthurHub/Android-Image-Cropper. Im having an issue on getting the cropper to popup. So right now Ive got a screen where I click a button and it takes me to my gallery and that is working perfectly fine. I can go to my gallery and all that. But when I click the image from the gallery, instead of it taking me to the crop editor, I go right back to the activity I was just at. This is my code
public class TestShareActivity extends AppCompatActivity {
private ImageButton imageButton;
private EditText mCaption;
private Button mShareBtn;
private static final int GALLERY_REQUEST = 1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_share);
imageButton = (ImageButton) findViewById(R.id.profileImageButton);
mCaption = (EditText) findViewById(R.id.addCaption);
mShareBtn = (Button) findViewById(R.id.uploadBtn);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST && requestCode == RESULT_OK){
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
}
}
I also have both of these in my Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Logcat from when I click the image button to go to my gallery to when I click the image I want which then takes me back to my activity
Suspending all threads took: 16.868ms
Recording user engagement, ms: 2309
Activity paused, time: 11770529
Logging event (FE): _e, Bundle[{_o=auto, _et=2309, _sc=TestShareActivity, _si=3274131924141379636}]
showStatusIcon on inactive InputConnection
Inactivity, disconnecting from the service
Using measurement service
Connecting to remote service
Activity resumed, time: 11775975
Connected to remote service
Timeline: Activity_idle id: android.os.BinderProxy#39c720c9 time:5790129
Processing queued up service tasks: 1
Inactivity, disconnecting from the service
I have 2 Activities the main activity I have called Map activity and the second that I have called Question activity when the app launch the Map activity is shown then you click the play button and the Question activity is launched with StartActivityForResult(), then when you have answered the question right the Question activity should be destroyed and created again and check the number of right answered questions and change the layout. then if you have scored 5 then Question activity should be destroyed and the Map activity will be shown but this is not happen. Here is my code
Map.class
public void OnClick_Question(View v){
Intent i = new Intent(MapActivity.this, QuestionActivity.class);
startActivityForResult(i, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(SharedPrefs.TAG, "OnActivityResult Entry");
if (resultCode == RESULT_OK) {
Log.d(SharedPrefs.TAG, "MAPActivity OnActivityResult Entry resultCode");
AssignContentView(requestCode);
}
}
Question.class
public void Next() {
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
public void LevelUp() {
Log.e(SharedPrefs.TAG, "LevelUp");
super.finish();
finish(); /*This don't happen*/
}
When I see LogCat shows me this message:
09-01 11:23:03.911 901-1485/? W/ActivityManager﹕ startActivity called from finishing ActivityRecord{43386100 u0 com.example.gbb/.QuestionActivity t50 f}; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=com.example.gbb/.QuestionActivity }
Why I am doing this, is because I want to change the layout of Map Activity depending in your score.
What I'm doing wrong?
I had the same problem. I solved it by adding android:launchMode="singleTask" in the Android Manifest. Hope it works for you too.
I solved this by turning off Instant Run and re-running my code.
You can try the below code:
We need to keep the request code to be in global and static variable to access in onActivityResult method
private static final int REQUEST_CODE = 111;
public void OnClick_Question(View v){
Intent i = new Intent(MapActivity.this, QuestionActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d(SharedPrefs.TAG, "OnActivityResult Entry");
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
Log.d(SharedPrefs.TAG, "MAPActivity OnActivityResult Entry resultCode");
AssignContentView(requestCode);
break;
default:
break;
}
}
}
I think there is no problem in your Map class, the problem is in second class.i.e., you need to set the RESULT_OK in that intent and then you can check if (resultCode == RESULT_OK) in onActivityResult method. Below you can find the code for that
Intent intent = new Intent();
setResult(activity.RESULT_OK, intent);
finish();
Hope this is helpful:)
It's a bit weird but maybe someone has an idea.
Activity A opens Activity B. In Activity B i press a button and it should be closed
returning to Activity A and then close it as well.
However, sometimes (1 to 7 times maybe) Activity B closes but Activity A remains.
Activity A
public void Foo() {
if (bIsVerifyClicked)
{
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(intent, 1000);
}
else
{
setResult(RESULT_OK);
finish();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(Logger.TAG, "=============requestCode: " + requestCode + " resultCode: " + resultCode);
if (resultCode == RESULT_OK)
{
setResult(RESULT_OK);
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
Activity B
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(Logger.TAG, "ActivityB ==== finish()");
setResult(RESULT_OK);
finish();
}
});
i thought maybe there is a memory leak in Activity B and thus the onResult get lost or something,
but it's java and i don't see anything strange.
do you have any lead what should i look for?
or how can i better debug this?
I am not able to figure out the exact issue.
But in your code i can see onActivityResult() of Activity A, no need of
setResult(RESULT_OK);
You can directly finish() activity A.