Activity甚至没有调用onActivityResult

时间:2016-03-10 10:51:09

标签: java android android-intent onactivityresult

我知道,我看过重复的内容。但他们都没有解决我的问题。

我希望我的代码从EditMessage活动中获取newMessage并将其传递给SendMessage Activity,我知道我可能不需要使用onActivityResult,我仍然想知道这里有什么问题。

我已经添加了日志消息以检查我的问题所在,但它甚至没有在onActivityResult中运行我的日志。

以下是代码:

EditMessageActivity:

public static final String MESSAGE = "message";

EditText currentTextEditText;
Button sendButton;
Button saveButton;
Button cancelButton;
Button concatenateButton;

private String message;

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

    message = getIntent().getStringExtra(SendMessageActivity.MESSAGE);

    currentTextEditText = (EditText) findViewById(R.id.currentText_EditText);
    sendButton = (Button) findViewById(R.id.sendButton);
    saveButton = (Button) findViewById(R.id.saveButton);
    cancelButton = (Button) findViewById(R.id.cancelButton);
    concatenateButton = (Button) findViewById(R.id.concatenateButton);

    if (message != null) //Activity may be started via "edit" Button
        currentTextEditText.setText(message);

    currentTextEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            message = currentTextEditText.getText().toString();
        }

        @Override
        public void afterTextChanged(Editable s) {
            message = currentTextEditText.getText().toString();
        }
    });

    concatenateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String[] messages = getResources().getStringArray(R.array.messages_array);

            AlertDialog.Builder builder = new AlertDialog.Builder(EditMessage.this);
            builder.setTitle("Sonuna Ekle").setItems(messages, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    char lastChar = message.charAt(message.length() - 1);
                    String messageToAdd = messages[which];
                    if (lastChar == '!' || lastChar == '?' || lastChar == '.') {
                        message += " " + messageToAdd;
                    } else {
                        message += " " + messageToAdd.toLowerCase();
                    }
                    currentTextEditText.setText(message);
                    dialog.cancel();
                }
            });
            builder.setCancelable(true);
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    });

    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(EditMessage.this, NumberSelectActivity.class);
            intent.putExtra(MESSAGE, message);
            startActivity(intent);
            finish();
        }
    });

    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Custom", "Save buttn clicked");
            Intent intent = new Intent(EditMessage.this, SendMessageActivity.class);
            intent.putExtra(MESSAGE, message);
            setResult(RESULT_OK);
            startActivityForResult(intent, SendMessageActivity.REQUEST_NEW_MESSAGE);
            finish();
            Log.i("Custom", "Custom Message created :" + message);
        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

}

SendMessageActivity:

public static final String MESSAGE = "message";
public static final int REQUEST_NEW_MESSAGE = 1001;

private static String message;

ListView messageListView;
Button createMessage;

ArrayList<String> messageList = new ArrayList<>();

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

    //Retrive Pre-Defined Messages
    //TODO Add in-app message defining
    String[] dbMessages = getResources().getStringArray(R.array.messages_array);

    for (int i = 0; i < dbMessages.length; i++) {
        messageList.add(dbMessages[i]);
    }

    //Create the array adapter
    //TODO Upgrade this to a custom adapter which will also show an small image related to the message
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, messageList);

    createMessage = (Button) findViewById(R.id.createMessageBtn);
    createMessage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startEditMessage("");
        }
    });

    messageListView = (ListView) findViewById(R.id.messagesListView);
    messageListView.setAdapter(adapter);

    //Make this one clickable
    messageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            message = messageList.get(position);

            AlertDialog.Builder builder = new AlertDialog.Builder(SendMessageActivity.this);
            builder.setTitle("Send Message");
            builder.setMessage(message);
            builder.setPositiveButton("Gönder", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(SendMessageActivity.this, NumberSelectActivity.class);
                    intent.putExtra(MESSAGE, message);
                    startActivity(intent);
                }
            });
            builder.setNeutralButton("Düzenle", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //TODO Create new activity to edit message and send it or just cancel it
                    //Todo and return back
                    startEditMessage(message);
                }
            });
            builder.setNegativeButton("İptal", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.setCancelable(true);
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.i("Custom", "Result OK değil :" + message);
    if (resultCode == RESULT_OK) {
        Log.i("Custom","Result OK:" + message);
        if (requestCode == REQUEST_NEW_MESSAGE) {
            Log.i("Custom","request is ok :" + message);
            String newMessage = getIntent().getStringExtra(EditMessage.MESSAGE);
            if (newMessage != null) {
                Log.i("Custom","Message is not null  :" + message);
                messageList.add(newMessage);

                //TODO Create new method to load all messages from database and just call that method
                ArrayAdapter<String> updatedAdapter = new ArrayAdapter<>(this,
                        android.R.layout.simple_list_item_1, android.R.id.text1, messageList);
                messageListView.setAdapter(updatedAdapter);

                Log.i("Custom", "Updated adapter :" + message);
            }

        }
    }
}

public void startEditMessage(String message) {
    Intent intent = new Intent(SendMessageActivity.this, EditMessage.class);
    intent.putExtra(MESSAGE, message);
    startActivity(intent);
}

任何帮助都会受到欢迎。谢谢。

2 个答案:

答案 0 :(得分:1)

  

我希望我的代码从EditMessage活动中获取newMessage并传递它   到SendMessage活动。

假设有三项活动A(MainActivity)B(EditMessageActivity)C(SendMessageActivity)

要从 B A 发送消息,请从 A 中获取startActivityForResult()。如果在 B 中检索到所需值,则在 B setResult(),然后调用finish()

结果将通过覆盖 onActivityResult() A 中收到。然后启动 C ,并将值放入intent。

例如,从MainActivity报告值EditMessageActivity

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Custom", "Save buttn clicked");
            Intent intent = new Intent();
            intent.putExtra(MESSAGE, message);
            setResult(RESULT_OK, intent);
            finish();
            Log.i("Custom", "Custom Message created :" + message);
        }
    });

如果只有两项活动,则不需要onActivityResult()。您可以使用SendMessageActivity直接将邮件传递给Intent

例如,在没有MainActivity的情况下将值从EditMessageActivity发送到onActivityResult()

saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Custom", "Save buttn clicked");
                Intent intent = new Intent(EditMessageActivity.this, SendMessageActivity.class);
                intent.putExtra(MESSAGE, message);
                startActivity(intent);
                Log.i("Custom", "Custom Message created :" + message);
            }
        });

答案 1 :(得分:0)

使用

int request = 0;// Any number to identify your request

startActivityForResult(intent,request);

而不是

startActivity(intent);

并且不要使用

  

完成()   ; //来自调用活动的函数,它将从堆栈中删除活动。