简体   繁体   中英

How can I get extra data by Intent using Android annotation?

I'm really new to Android. I'm trying to pass the data to the text view of another class. But for some reason, it does not appear in the second class / page (the textview didnt change). I was able to do it when I was not using android annotation, but I was told that using annotations is a lot easier so I'm trying to convert it to that but for some reason it isn't working? I might be missing something.

Main 1

package com.example.faculty.labactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.world)
public class MainActivity extends AppCompatActivity {

    @ViewById(R.id.userName)
    EditText user;


    @Click(R.id.signIn)
    public void signInButton(){

     Main2Activity_.intent().name(user.getText().toString()).start();

   }

Main 2

package com.example.faculty.labactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.welcome)
public class Main2Activity extends AppCompatActivity {

@Extra("name")
String n;

@ViewById(R.id.name)
TextView tv;

Intent in2 = getIntent();

@AfterViews
void home(){
    tv.setText("Hello, " + n + " !");
    }

}

In your receiving Activity (for example MyActivity) you must declare the name of the object you want to receive like that

@Extra("name")
String name;

To pass data, when you create your intent you must do something like this:

Main2Activity_.intent(this).name(user.getText().toString()).start();

If you need a more precise solution edit your question to show more of your code.

You can look at the official doc for a more complete example

It might have to do with the dependencies you should add for the annotations to your project_root/build.gradle and app/build.gradle files.

Make sure to check this thread: what is Android Annotations and why we use Android Annotations?

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