How to solve this issue i got only in Moto g? - android

i am try to choose image form gallery and crop the image.i tested more than 5 devices i got only problem with Moto g.how to solve this problem?i am using TabGroupActivity
my code
parent activity
public class TabGroup1Activity extends TabGroupActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("OptionsActivity", new Intent(this,ProfileActivity.class));
}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) throws ClassCastException {
if (requestCode == 1) {
Family activity = (Family)getLocalActivityManager().getCurrentActivity();
activity.onActivityResult(requestCode, resultCode, intent);
}
else if (requestCode == 2) {
Home activity = (Home)getLocalActivityManager().getCurrentActivity();
activity.onActivityResult(requestCode, resultCode, intent);
}
child activity:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);`enter code here`
getParent().startActivityForResult(
Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);`
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {`switch (requestCode) {
case SELECT_PICTURE:
try {
mImageCaptureUri = data.getData();
doCrop();
} catch (Exception e) {
e.printStackTrace();
}
break;
}}
`

Note that your activity may be recreated on posting result from gallery or camera.Please save your current state of the activity in onSavedInstatnceStae() and restore the same if your activity is recreated. Refer Here for more info
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("ImageUri", imageUri);
}
protected void restoreSavedInstanceState(Bundle savedInstanceState) {
imageUri = savedInstanceState.getString("ImageUri");
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null)
{
restoreSavedInstanceState(savedInstanceState);
}
}

Related

onActivityResult not being called in calling activity

I have a FragmentActivity that is starting another activity for result. When the called activity finishes, onActivityResult is not called. Does it make a difference that I am using a AppCompatActivity activity (which extends from FragmentActivity)? The documentation says that results will be returned to the calling fragment, and in this case it's not a fragment, it's an activity. Here is the code, very simple:
MainActivity:
public class SMSEmailActivityNew extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setup activity....
Intent i = new Intent(this, EulaActivity.class);
i.putExtra(Globals.keyFileName,Globals.FILE_EULA );
startActivityForResult(i,RESULT_OK);
}
//this method is never called
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//dowork .....
}
}
Called activity:
EulaActivity extends AppCompatActivity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up activity ....
}
public void onClick(View v) {
Intent result = new Intent();
if (bPermissionGranted) {
setResult(Activity.RESULT_OK, result);
// Determine if EULA was accepted this time
getSharedPreferences().edit().putBoolean(Globals.KEY_EULA_ACCEPTED, true).apply();
} else {
setResult(Activity.RESULT_CANCELED, result);
}
finish();
}
}
According to documentation you need to pass requestId bigger or equal thant 0. In your case RESULT_OK is -1. Also RESULT_OK acts like result code, not like request code and startActivityForResult needs a request code.
Something like this startActivityForResult(intent, 0);
Also finish EulaActivity using finishActivity(yourPreviousRequestCode);, in this case 0.
Try this solution:-
MainActivity.java
//Define variable
public static int REQUEST_CODE = 233;
public class SMSEmailActivityNew extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setup activity....
Intent i = new Intent(this, EulaActivity.class);
i.putExtra(Globals.keyFileName,Globals.FILE_EULA );
startActivityForResult(i, REQUEST_CODE); //Change here
}
//this method is never called
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE) {
if(resultCode == RESULT_OK) {
if(data != null && data.hasExtra("MESSAGE")) {
String resStr = data.getStringExtra("MESSAGE");
Toast.makeText(MainActivity.this, resStr, Toast.LENGTH_SHORT).show();
}
}else if(resultCode == RESULT_CANCELED)
Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show();
}
}
}
EulaActivity.java
EulaActivity extends AppCompatActivity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up activity ....
}
public void onClick(View v) {
Intent result = new Intent();
result.putExtra("MESSAGE","Eula Accepted Set");
if (bPermissionGranted) {
setResult(Activity.RESULT_OK, result);
// Determine if EULA was accepted this time
getSharedPreferences().edit().putBoolean(Globals.KEY_EULA_ACCEPTED, true).apply();
} else {
setResult(Activity.RESULT_CANCELED, result);
}
finish();
}
}

I can not catch request code

I have a problem with the response of my activity. It is a module to react natively. I have this react class and I am trying to get result from my activity. I've tried catching the requestCode like this:
public CameraOpenerModule(ReactApplicationContext reactContext) {
super(reactContext);
getReactApplicationContext().addActivityEventListener(new ActivityEventListener() {
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("TEST", "listener start event");
if (requestCode == 9999) {
Log.e("TEST", "request code ");
}
}
});
}
and I open the class intent:
private void openCameraIntent(){
cameraIntent = new Intent(getReactApplicationContext(), CameraHelpActivity.class);
// cameraIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getReactApplicationContext().startActivityForResult(cameraIntent, 9999,null);
}
and another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(1111);
Log.i("ACTIVITY", "finish !!!!!");
finish();
}
Everything is ok. But I'm not catching the request code. Does anybody know why? Thanks
Add to MainActivity.java
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mReactInstanceManager != null){
mReactInstanceManager.onActivityResult(requestCode,resultCode,data);
}
}

I need to wait until an activity completes before proceeding, but only sometimes

My application starts with InitActivity that checks for login status, calls LoginActivity accordingly before proceeding to some logic.
I know I can use the startAcivityForResult() and onActivityResult() to ensure LoginActivity completes before doing doSomeMainLogic(), but my if check throws a curve in it. If I do this:
public class InitActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!userIsLoggedIn) {
Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
startActivityForResult(intent,1);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
doSomeMainLogic();
}
}
}
}
how do I get doSomeMainLogic() to fire if the user is already logged in?
Thanks much.
public class InitActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!userIsLoggedIn) {
Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
startActivityForResult(intent,1);
}
else {
doSomeMainLogic(); // this part is added
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
doSomeMainLogic();
}
}
}
}

android - How to use startActivityForResult() in tabs

My application have 3 tabs. In this one tab have multiple activities(means this tab have navigation to the child tabs). I used the startActivityforResult() in one child activity. But control never goes to onActivityResult() method. How to implement this. please can anybody help me.
code
public class Activity_1 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View contentView = LayoutInflater.from(getParent()).inflate(R.layout.static_search_filters, null);
setContentView(contentView);
states_tv = (TextView)findViewById(R.id.state);
states_tv.setOnClickListener(states_etListener);
}
private OnClickListener states_etListener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getParent(), RB_CategoriesMList.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent,GET_SEL_STATES_LIST);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//I do some stuff here
}
}
//Activity_2
public class RB_CategoriesMList extends ListActivity
{
public Button sbtn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
sbtn =(Button)findViewById(R.id.submit_categories);
sbtn.setOnClickListener(sbtn_listener);
}
private OnClickListener sbtn_listener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent state_intent = getIntent();
state_intent.putExtra("selected_states", "");
setResult(RESULT_OK,state_intent);
finish();
}
};
}
Dont call
super.onActivityResult(requestCode, resultCode, data);
When you receive the onActivityResult, check if the request code is your and then do your stuff. In the other cases you should call the super.
So:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == GET_SEL_STATES_LIST) {
// Do your stuff
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
getParent().startActivityForResult(yourNewIntent, yourResult);

OnActivityResult not working (Android)

I am using a simple function OnActivityResult, but it is not returning me desired results.
Please see my code and tell me where i am doing wrong.
public void OnClickFunction(View view)
{
Intent intent = new Intent(getApplicationContext(), Second.class);
startActivityForResult(intent, RESULT_OK);
/// My actions. . .
}
Then in the Second class, i have set Result like this:
Button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ValueOne = EditText.getText().toString().trim();
if (ValueOne.equals(String.valueOf(Answer)))
{
Toast.makeText(getApplicationContext(), "Correct Answer", 0).show();
Second.this.setResult(RESULT_OK, null);
Second.this.finish();
}
else
{
Toast.makeText(getApplicationContext(), "Wrong Answer", 0).show();
}
}
});
Now coming back to the first.class, from where the Intent was called:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == RESULT_OK)
// {
if (resultCode == RESULT_OK)
{
///// MyActions. . .
}
// }
}
The debugger is not debugging this function, so the desired results are not coming.
Where i am doing wrong??
You have to destroy the second activity. Try pressing back button. I am able to see all the log messages in onActivityResult
First Activity
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
int result = 100;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this,SecondActivity.class);
startActivityForResult(i, result);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Log.i("H", "RequestCode:" + requestCode);
Log.i("H", "ResultCode:" + resultCode );
}
}
SecondActivity
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(RESULT_OK);
Log.i("S","Exiting Second Activity");
}
}
in Source Class:
int activity=1;
Intent i=new Intent(Sourceclass.this,destination.class);
startActivityForResult(i,activity);
In Destination class:
Intent i=new Intent();
setResult(RESULT_OK,i);
finish();
In OnActivityResult of Source Class:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode==1)
{
Log.e("check","check");
}
}
}
in my case: when use activityForResult, I add this flag :
addFlags(FLAG_ACTIVITY_NEW_TASK)
when delete this flag ,fixed

Categories

Resources