0

So i'm trying to launch an Activity when the user is done writing. But instead of launching the Activity, it seems to run the Program again. I'm saying this cause instead of launching the right Activity it launches the first Activity of my program.

search.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH ||
            actionId == EditorInfo.IME_ACTION_DONE ||
            event.getAction() == KeyEvent.ACTION_DOWN &&
            event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            if (!event.isShiftPressed()) {
                // the user is done typing.
                text = search.getText().toString();
                new SearchActivity(text);
                Intent it = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(it);
                return true; // consume.
            }
        }
        return false;
    }
});

Here is the code for the SearchActivity

public class SearchActivity extends AppCompatActivity {
    private String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_activity);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    public SearchActivity(String text) {
        this.text = text;
    }
}

Thank you in advance

Dhruv
  • 1,801
  • 1
  • 15
  • 27
Raidline
  • 81
  • 9

2 Answers2

0

Here is a example I use.

startActivity(new Intent(this, AboutActivity.class));
 return true;'

Try changing your code to this

Intent it = new Intent(this, SearchActivity.class);
  • I can't just use this cause i'm on another class the EditText.OnEditorActionListener so just puting this will send me to the classe i'm at. I need to use the MainActivity.this. Thank you anyways – Raidline May 07 '16 at 09:03
0

So finally i was able to correct the issue. First i took out the if (!event.isShiftPressed()), it was sending and error the the event is null. I noticed that is fine since the program still works the way i want that to. The second problem was that when i launch the activity it was giving the error(class has no zero arguments).By solving this i put and empty parametrer constructor array. I've learned this from this thread. There it explains why i should do this.

Here i put my new code.

search.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    event.getAction() == KeyEvent.ACTION_DOWN &&
                            event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    // the user is done typing.
                    text=search.getText().toString();
                    new SearchActivity(text);
                    Intent it = new Intent(MainActivity.this, SearchActivity.class);
                    startActivity(it);
                    return true; // consume.
            }
            return false;
        }
    });

Now the SearchActivity

public class SearchActivity extends AppCompatActivity {
private String text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_activity);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

public SearchActivity(String text){
    this.text=text;
}

public SearchActivity(){
    this.text=null;
}

}

Community
  • 1
  • 1
Raidline
  • 81
  • 9