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 "";
}