A
A
Abejon2018-02-16 06:44:07
Android
Abejon, 2018-02-16 06:44:07

Android: Why does intent come with empty Extras?

There are two activities in the application. login:

package com.myapp.myappardandroid;
 
import android.arch.persistence.room.Room;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
 
import java.util.ArrayList;
import java.util.List;
 
import static com.mayapp.myappandroid.Constants.SETTINGS;
 
public class LoginActivity extends AppCompatActivity {
 
    private SettingsDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
 
        Intent intent = getIntent();
 
        ImageButton settingsBtn = findViewById(R.id.settingsBtn);
        settingsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateSettings(view);
            }
        });
 
        db = Room.databaseBuilder(getApplicationContext(), SettingsDatabase.class, "settingsDb")
                .allowMainThreadQueries()
                .build();
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
 
        if (resultCode == RESULT_OK) {
            db.getSettingsDao().insert(new Settings("apiEndPoint", data.getStringExtra("apiEndPoint")));
            db.getSettingsDao().insert(new Settings("apiKey", data.getStringExtra("apiKey")));
        }
    }
 
    private void updateSettings(View view) {
        Intent intent = new Intent(LoginActivity.this, SettingsActivity.class);
        List<Settings> settingsList = db.getSettingsDao().getAllSettings();
        for(int i = 0; i < settingsList.size(); i++){
            Settings settings = settingsList.get(i);
            intent.putExtra(settings.getParamName(), settings.getParamValue() == null ? "" : settings.getParamValue());
        }
        startActivityForResult(intent, SETTINGS);
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.myapp.myappandroid.LoginActivity">
 
    <!-- Login progress -->
    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />
 
    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
 
            <android.support.v7.widget.AppCompatImageButton
                android:id="@+id/settingsBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:src="@drawable/settings_icon"
                android:background="@android:color/transparent" />
 
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
 
                <AutoCompleteTextView
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_email"
                    android:maxLines="1"
                    android:singleLine="true" />
 
            </android.support.design.widget.TextInputLayout>
 
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
 
                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_password"
                    android:imeActionId="6"
                    android:imeActionLabel="@string/action_sign_in_short"
                    android:imeOptions="actionUnspecified"
                    android:inputType="textPassword"
                    android:maxLines="1"
                    android:singleLine="true" />
 
            </android.support.design.widget.TextInputLayout>
 
            <Button
                android:id="@+id/email_sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/action_sign_in"
                android:textStyle="bold" />
 
        </LinearLayout>
    </ScrollView>
</LinearLayout>

And settings:
package com.myapp.myappandroid;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class SettingsActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
 
        Intent intent = this.getIntent();
        EditText apiEndPoint = findViewById(R.id.settingsApiEndPoint);
        apiEndPoint.setText(intent.getStringExtra("apiEndPount"));
        EditText apiKey = findViewById(R.id.settingsApiKey);
        apiKey.setText(intent.getStringExtra("apiKey"));
 
        Button ssBtn = findViewById(R.id.saveSettingsBtn);
        ssBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveSettings(view);
            }
        });
    }
 
    public void saveSettings(View view) {
        Intent answerIntent = new Intent(SettingsActivity.this, LoginActivity.class);
        EditText apiEndPoint = findViewById(R.id.settingsApiEndPoint);
        answerIntent.putExtra("apiEndPoint", apiEndPoint.getText());
        EditText apiKey = findViewById(R.id.settingsApiKey);
        answerIntent.putExtra("apiKey", apiKey.getText());
 
        setResult(RESULT_OK, answerIntent);
        finish();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.myapp.myappandroid.SettingsActivity">
 
    <EditText
        android:id="@+id/settingsApiEndPoint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/settings_end_point_hint"/>
 
    <EditText
        android:id="@+id/settingsApiKey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/settings_api_key"/>
 
    <android.support.v7.widget.AppCompatButton
        android:id="@+id/saveSettingsBtn"
        style="?android:textAppearanceSmall"
        android:layout_marginTop="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/setings_save_button_text"
        android:textStyle="bold" />
 
</android.support.v7.widget.LinearLayoutCompat>

Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.myappandroid">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:label="List of Mobile OS"
            android:name=".LoginActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SettingsActivity" />
    </application>

</manifest>

I'm trying to pass parameters from one to another. Before sending, I check with a debugger - in both cases, the intent in Extras contains the entered values. At the receiving end, I check - Extras are empty in both cases. That is, it does not contain empty values ​​with the specified names, but empty, from the word "generally".
What can be wrong? What am I doing wrong? Where to see? Where to dig?
PS I don't know how important this is, but LoginActivity was originally google LoginActivity, I removed everything except data entry fields.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abejon, 2018-02-19
@Abejon

It turned out that getText does not return a string value, but an Editable. Correct dj second activity write like this:
answerIntent.putExtra("apiEndPoint", apiEndPoint.getText(). toString() );
The addendum is in bold. Those. I put in Extras a non-primitive type, as required.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question