3

First of all:

ALL of the answers of the related questions did not solved my problem, thats why i am creating a new question.

Explanation:

I have a huge android app, i use Eclipse-ADT plugin and i am using a smartphone that have android 4.0.4 to do the tests, i have a lot of Activity's and i pass values between Activity's via Bundle getExtras in onCreate() and too i get some values on onActivityResult's.

Obs:

(1) = Activity A that calls for result -> Activity B

(2) = Activity B that get values from Activity A and returns other values to -> Activity A.

Problem:

  1. I have ProdutosForm.java(1) activity and PedidoItemx.java(2) activity.
  2. Inside ProdutosForm.java(1), i am starting PedidoItemx.java(2) activity to get a result:

Code for item 2:

<!-- language: lang-java -->

Intent i = new Intent();
i.setClass(this, PedidoItemx.class);
i.putExtra("CODE", code );

...
... (a LOT of putExtras....)
...

//and after putExtras, thats the relevant part
startActivityForResult(i, 1); //i its the Intent setted as PedidoItemx.class
  1. Inside PedidoItemx.java(2) onCreate method i have:

Code for item 3:

<!-- language: lang-java -->

Bundle extras    = getIntent().getExtras();
code             = extras.getString("CODE");

4. Too, still inside PedidoItemx.java(2) i set some new values and i set the result to RESULT_OK:

Code for item 4

<!-- language: lang-java -->

Intent i = getIntent();
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i); //RESULT_OK is a native constant that have value -1
finish();
  1. Now, returning to ProdutosForm.java(1) i have the following onActivityResult code to get the values returned by PedidoItemx.java(2):

Code for item 5:

<!-- language: lang-java -->

public void onActivityResult(int requestCode,int resultCode,Intent data){
    if(resultCode==RESULT_OK && requestCode==1){
      Bundle MBuddle = data.getExtras();
      Intent i = getIntent();
      String cPedidoExtra = MBuddle.getString("PEDIDO");
      i.putExtra("PEDIDO", cPedidoExtra);
      i.putExtra("VLRFRETE", MBuddle.getFloat("VLRFRETE", 0));
      cPedPedido = cPedidoExtra; //this does nothing important
      setResult(RESULT_OK, i);
    }
}
  1. Everything looks OK and should work, but it doesn't. BECAUSE my requestCode is always 0 and my data is always null thats the real problem.

My manifest:

<!-- language: lang-xml -->

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bee.mobile"
    android:versionCode="2"
    android:versionName="1.50" >
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" /> 
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCOUNT_MANAGER" />           
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <!-- <uses-permission android:name="android.permission.RECEIVE_SMS" /> -->

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:screenOrientation="user"
        android:debuggable="true"> <!-- mudar para false ao gerar depois -->

        <activity
            android:name="bee.mobile.form.outros.SplashActivity"
            android:label="@string/app_name"            
            android:screenOrientation="sensor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Outros -->
        <activity android:name="bee.mobile.form.outros.MainActivity"/>
        <activity android:name="bee.mobile.form.outros.HistoricoClienteForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoListaNotasForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoNotaForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoProdutoForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoMesProdForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoQuemForm"/>
        <activity android:name="bee.mobile.form.outros.PedidoItemx"/>
        <activity android:name="bee.mobile.form.outros.RelatoriosForm"/>
        <activity android:name="bee.mobile.form.outros.EstufaDetalhes"
            android:windowSoftInputMode="stateHidden"/>
        <!-- Lista -->             
        <activity android:name="bee.mobile.form.lista.ClientesForm"/>
        <activity android:name="bee.mobile.form.lista.ProdutosForm"/>
        <activity android:name="bee.mobile.form.lista.PedidoListaForm"/>
        <activity android:name="bee.mobile.form.lista.ContatosListaForm"/>
        <activity android:name="bee.mobile.form.lista.GruposForm"/>
        <activity android:name="bee.mobile.form.lista.NotaListaForm"/>
        <!-- Cadastro -->
        <activity android:name="bee.mobile.form.cadastro.PedidoCadForm"/>
        <activity android:name="bee.mobile.form.cadastro.ClientesCadastroForm"
            android:windowSoftInputMode="stateHidden"/>        
        <activity android:name="bee.mobile.form.cadastro.ContatosCadForm"
            android:windowSoftInputMode="stateHidden"/>
        <activity android:name="bee.mobile.form.cadastro.PedidoCadForm_Financeiro"/>
        <!-- Sem destino -->        
        <activity android:name="bee.mobile.ConfigurarForm"/>
        <activity android:name="bee.mobile.ConfiguracaoLocalForm"/>
        <activity android:name="bee.mobile.AtualizaManual"/>                        
        <activity android:name="bee.mobile.ReceberForm"/>                              
        <activity android:name="bee.mobile.ResolucaoForm"/>
        <activity android:name="bee.mobile.AtualizarForm"/>
        <activity android:name="bee.mobile.ParticipantesVendaForm"/>       
    </application>

</manifest>

Question:

Why i cant get the data returned from Activity B?? i am doing something wrong?

Paulo Roberto Rosa
  • 3,071
  • 5
  • 28
  • 53

3 Answers3

1

I figured out the solution for my problem:

I dont know WHY but if i MOVE this piece of code that is actually on buttonClick event on Activity B(2):

Intent i = getIntent();
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i); //RESULT_OK is a native constant that have value -1
finish();

To -> right after onCreate, it works ok.

But if i move back this code "to the right place" on a button click, it doesnt work, i really dont know why the activity is having this very weird behaviour, but i'll be happy to share when i figure it out.

In other words, i have to set the extras and the RESULT_OK before doing anything, otherwise it didnt work.

Community
  • 1
  • 1
Paulo Roberto Rosa
  • 3,071
  • 5
  • 28
  • 53
  • does this help you - https://stackoverflow.com/questions/6322874/onactivityresult-has-intent-data-as-null-after-an-activity-has-finished – mrtechmaker Dec 29 '18 at 13:45
0

I think you should add an action ACTION_SEND to your intent to return to the other activity. Something like this:

Intent i = getIntent();
**i.setAction(Intent.ACTION_SEND);**
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i);
finish();
Micho
  • 3,929
  • 13
  • 37
  • 40
0

Use:

Intent i = this.getIntent();

instead of

Intent i = getIntent();

in the activity where you need to receive data of the intent, which has the values binded to some keys.

Harpreet
  • 2,990
  • 3
  • 38
  • 52