Home > OS >  Paste is not working on Oncreate? Android Clipboard
Paste is not working on Oncreate? Android Clipboard

Time:06-18

There is a strange behavior of Android copy paste. If I use handler paste is working but on oncreate and onstart it is not.

Activity

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

    Log.e("TAG", "onCreate1: "   C.paste(Verify_files.this) );
    new Handler(getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {

            Log.e("TAG", "onCreate2: "   C.paste(Verify_files.this) );
        }
    },400);

Logs are here. primary clip is false then it becomes true

E/##CC: paste: false
E/TAG: onCreate1: 
E/##CC: paste: true
E/TAG: onCreate2: THIS IS TEXT TO PASTE

Copy() Paste()

    public static void copy(Context context , String text){
    Log.e(TAG, "copy: " text );

    ClipboardManager clipboard = (ClipboardManager) context.getSystemService( CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("label", "THIS IS TEXT TO PASTE");
    clipboard.setPrimaryClip(clip);

    Toast.makeText(context,text "",Toast.LENGTH_SHORT).show();
}

public static String paste(Context context  ){
    ClipboardManager clipboard= (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);

    Log.e(TAG, "paste: "   clipboard.hasPrimaryClip() );

    if(clipboard.hasPrimaryClip()) {
        ClipData clip = clipboard.getPrimaryClip();
         if (clip != null) {
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
            return item.getText()   "";
        }
    }
    return  "";

}

CodePudding user response:

There are a few approached you can do here.

First, by using ViewTreeObserver. Refer: https://stackoverflow.com/a/8741821/9346054

private void listenToViewTreeObserver(){
    final ViewTreeObserver vto = findViewById(R.id.text_view_something).getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // do something now when the object is loaded
            // e.g. find the real size of it etc
            Log.e("TAG", "onCreate: onGlobalLayout: "   paste(MainActivity.this) );
            findViewById(R.id.text_view_something).getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

Second, by using MessageQueue. https://stackoverflow.com/a/41276825/9346054

private void listenToMessageQueue(){
   final MessageQueue.IdleHandler handler = () -> {
        Log.e("TAG", "onCreate: IdleHandler: "   paste(MainActivity.this) );
        return false;
    };
    Looper.myQueue().addIdleHandler(handler);
}

At your onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    copy(MainActivity.this,"Here");

    listenToMessageQueue();
    listenToViewTreeObserver();
}
  • Related