Search code examples
androidandroid-activitybundle

Get string from bundle android returns null


I want to pass a string from one activity to another, in one of them I wrote

public void pdfView(File f) {

 // f is: /data/data/com.example.iktabClasses/files/fileName.pdf

 Intent intent = new Intent(getApplicationContext(),NewPdfActivity.class);

 intent.putExtra("filename", f);

    startActivity(intent);

}

and in the other Activity I wrote:

  Bundle b=getIntent().getExtras();

        if (b != null) {

        filename = getIntent().getStringExtra("filename");

       System.out.println("filename: "+filename);
    } 

but filename always returns as 'null'. How to solve this? Thanks in advance. //////////////////

I made it as

   Intent intent;
    Bundle b = new Bundle();

    b.putString("filename", f.toString());

    intent = new Intent(getApplicationContext(),NewPdfActivity.class);

    intent.putExtras(b);

    startActivity(intent);

and Now it work


Solution

  • try this way

    Intent intent = new Intent(first.this, second.class);
    
    Bundle bundle = new Bundle();
    bundle.putInt("index", index);
    
    intent.putExtras(bundle);startActivity(intent);
    

    then get it as

    Bundle b = getIntent().getExtras();
    int index = b.getInt("index");