简体   繁体   中英

How can I pass data to the activity in my app?

If I press the share button that appears by long pressing the text, I want to send the text to other activities of my app. I move to another activity, but the text I want is not delivered. What is the problem?

This is the code of the activity that writes the text I want to deliver.

public class MainActivity extends AppCompatActivity {

EditText editText;
Intent sendIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText);
    sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_VIEW);
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendIntent.putExtra("TEXT", editText.getText().toString());
            sendIntent.setType("text/*");
        }
    });
}

} This is the intent-filter of my xml code.

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <action android:name="android.intent.action.MAIN"/>

    <category android:name="android.intent.category.LAUNCHER" />
    <data android:mimeType = "text/*"/>
</intent-filter>

This is the part that receives the intent of the activity that I want to receive data from.

Intent receiveIntent = getIntent(); url = receiveIntent.getStringExtra("TEXT");

webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url); This is the intent-filter part of the xml code of the activity where I want to receive data.

<activity android:name=".MyWebBrowser"
    android:label="MyWebBrowser">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />


    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/*"/>
    </intent-filter>

Here is a simple example for you:

Instead of long press on editText I simply use a button, but nevertheless, the logic stays almost the same:

// MainActivity. From here we send the text from EditText to SecondActivity.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        EditText editText = findViewById(R.id.editText);
        Intent intent = new Intent(this, SecondActivity.class);
        intent.setAction("MyIntentAction");

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("TEXT", editText.getText().toString());
                startActivity(intent);
                finish();
            }
        });
    }
}

// SecondActivity. Receives and displays text from MainActivity.

    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            TextView textView = findViewById(R.id.textView);
    
            Intent intent = getIntent();
            if(intent.getAction().equals("MyIntentAction")) {
                String str = intent.getStringExtra("TEXT");
                textView.setText(str);
            }
    
        }
    }

// For Long Click detection add the following code to MainActivity:

editText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                intent.putExtra("TEXT", editText.getText().toString());
                startActivity(intent);
                finish();
                return true;
            }
        });

I see that you are using two activity has LAUNCHER with the MAIN in your manifest. Please use only one in your manifest .

To pass the data to another activity in your app, you can try to use startActivity and put your data in the intent data:

  • On action to open the MyWebBrowser :

     val intent = Intent(context, MyWebBrowser::class.java) intent.putExtra("TEXT", yourtext) startActivity(intent)
  • Get the Text in your MyWebBrowser :

    override fun onCreate(savedInstanceState: Bundle?) {
       ...
       val TEXT = intent.getStringExtra("TEXT")
       textView.text = TEXT
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM