user1803817
user1803817

Reputation: 3

android studio. Nothing happens when i use intent.putextra

I am new in this world. And im followering online guides to learn the language.

I am trying to get a notepad to work but i cant reopen the notes already created. I use the debugger but it seems to call the activity but it never opens.

The call.

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
            View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

            if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
                int position = recyclerView.getChildLayoutPosition(child);
                Note selectedNote = mNotes.get(position);
                Intent editorIntent = new Intent(getActivity(), NoteEditorActivity.class);
                editorIntent.putExtra("id", selectedNote.getId());
            }
            return false;
        }

I can see editorIntent.putExtra("id", selectedNote.getId()); is calling the action with a id but when i look at the part who recieves the intent nothing happens.

public class NoteEditorActivity extends AppCompatActivity {

private Toolbar mToolbar;

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


    mToolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //remove this line in the MainActivity.java

    if (savedInstanceState == null){
        Bundle args = getIntent().getExtras();
        if (args != null && args.containsKey("id")){
            long id = args.getLong("id", 0);
            if (id > 0){
                openFragment(NotePlainEditorFragment.newInstance(id), "Editor");
            }
        }
        openFragment(NotePlainEditorFragment.newInstance(0), "Editor");
    }
}

But in this code nothing react.

Can someone give me a hint??

Regards Danni.

Upvotes: 0

Views: 118

Answers (1)

Mayank Bhatnagar
Mayank Bhatnagar

Reputation: 1346

Change this code

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

        if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
            int position = recyclerView.getChildLayoutPosition(child);
            Note selectedNote = mNotes.get(position);
            Intent editorIntent = new Intent(getActivity(), NoteEditorActivity.class);
            editorIntent.putExtra("id", selectedNote.getId());
        }
        return false;
    }

To

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

        if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
            int position = recyclerView.getChildLayoutPosition(child);
            Note selectedNote = mNotes.get(position);
            Intent editorIntent = new Intent(getActivity(), NoteEditorActivity.class);
            editorIntent.putExtra("id", selectedNote.getId());
            startActivity(editorIntent);
        }
        return false;
    }

You need to call startActivity to begin the transaction from current activity to desired activity

And try to get id like this in next activity, use this code inside onCreate()

Intent intent = getIntent(); 
String id = intent.getStringExtra("id");

Upvotes: 1

Related Questions