0

My code flow is:

Reports => ReportsType

Reports is having 3 items and on click of each item, I am starting the activity ReportsType passing the tag with the name name in the intent to distinguish which item was clicked.

The problem is OnCreate method is called only once, so heading is always set to what item was clicked in the starting.

public void onCreate(Bundle si)
    {
        Intent intent = getIntent();
        heading = intent.getExtras().getString("name"); //this tells which item was clicked.
        TextView heading_txt = (TextView) findViewById(R.id.heading);
        heading_txt.setText(heading);
     }

I tried to put this code at onResume() call because it is called everytime the activity resumes. But still the getIntent() is giving the old name value set from previous item.

How to get the current clicked item intent value in the other activity?

UPDATE:

Reports activity code:

   public void showReport(View v) {
    String tag = v.getTag().toString();
    Intent i5 = new Intent(this, ReportsType.class);
    i5.putExtra("name", tag);
    startActivity(i5);

}

Where showReport() method is called each item you click any of the three items.

UPDATE:

goBackReport code

public void goBackReport(View v)
    {

    Intent intent = new Intent(ReportsType.this, Reports.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
        finish();
    }

XML button view

<Button
        android:id="@+id/entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_blue_xml"
        android:clickable="true"
        android:padding="8dp"
        android:onClick="goBackReport"
        android:text="Back"
        android:textColor="#ffffff"
        android:textSize="15dp" />
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Post your Reports Activity code too – Arun C Apr 09 '13 at 11:05
  • I have understood your question, but I have a confustion regarding `problem is OnCreate method is called only once, so heading is always set to what item was clicked in the starting` are you pressing back button? – Pragnani Apr 09 '13 at 11:06
  • Make sure that you pass correct View to ShowReport() and the tag is different for each views – Arun C Apr 09 '13 at 11:09
  • @Pragnani correct I am pressing back button in `ReportsType` activity which again sends me to `Reports` activity. Then I click the next item but the value I get from tag is of the first item. Because `OnCreate()` method is called only for first item or only once. – sjain Apr 09 '13 at 11:12
  • When you click BACK in `ReportsType`, that activity will be finished and you will return to `Reports` activity. If you then call `startActivity()` to start `ReportsType` again, for sure `ReportsType.onCreate()` will be called. There is something else going on here. Add debug logging in `ReportsType.onCreate()` to see what it gets in the `Intent` and add debug logging in `showReport()` to see when it is called and what you are sending. – David Wasser Apr 09 '13 at 11:24
  • My `Reports` and `ReportsType` classes are not extending `Activity` but they are extending `FragmentActivity`. Is that something to do with that? Morever, my onBackPressed() is never getting called. So I am calling some method when click on back button which is directly defined in ReportsType xml. And in that method I called `finish()` which finishes all the fragmentActivities and I see the homescreen rather than `Reports` class view. – sjain Apr 09 '13 at 11:30
  • What method are you calling when the user clicks the BACK button in `ReportsType`? Why? Post the code of that method. – David Wasser Apr 09 '13 at 11:47
  • @Pragnani My back button is not calling `onBackPressed()`. I used your code in a method other than `onBackPressed()` and I am getting a slight delay on a call to `finish()` with a blank screen. – sjain Apr 09 '13 at 11:48
  • I am calling `goBackReport(View v)` method when the user clicks on back button because `onBackPressed()` is not getting called on back button click. In xml of button I defined - `android:onClick="goBackReport"`. See the code of the `goBackReport` in the question. – sjain Apr 09 '13 at 11:51
  • @SaurabhJain is your code working , as you haven't setContentView and you are trying to get the TextView, so you'll probably get `NPE` – Pragnani Apr 09 '13 at 11:54
  • @Pragnani But how the activity will know that its the back button clicked just by name. I mean there may be lots of buttons with different functionalities. How it will distinguish. – sjain Apr 09 '13 at 11:54
  • @Pragnani Ok see my button view in the question updated. – sjain Apr 09 '13 at 12:00
  • I have already set the `contentview` and that's why I am seeing my view of `ReportsType` class. – sjain Apr 09 '13 at 12:10
  • Why do you need a software BACK button? All devices have a hardware BACK button. If you really need a software BACK button, then `goBackReport()` should just call `finish()`. That's it. – David Wasser Apr 09 '13 at 12:12
  • @SaurabhJain So my answer doesn't provide answer to your question..So I am deleting it... – Pragnani Apr 09 '13 at 12:15
  • @DavidWasser It was just understanding problem...I really appreciate your support.. – Pragnani Apr 09 '13 at 12:16
  • @DavidWasser If I call just `finish()` then it finishes all the `Reports` and `ReportsType` activity classes and I see the `MainFragmentActivity` class view. – sjain Apr 09 '13 at 12:19
  • Then I don't understand. When `Reports` starts `ReportsType` does it then call `finish()`? It shouldn't. – David Wasser Apr 09 '13 at 12:27
  • It doesn't call `finish()` as you see in the `showReport()` method. That's what I have to start `Reports` activity. – sjain Apr 09 '13 at 12:31
  • Still don't understand. Your `Reports` activity is running. It starts `RerportsType` activity and does NOT call `finish()` on itself. That means `Reports` activity is still in the activity stack in paused state. Now, when you call `finish()` in `ReportsType` activity, that will remove `ReportsActivity` from the activity stack and return to the activity underneath it, which should be `Reports` activity, where it will call `onResume()`. That's what should happen. You can verify what activities are in the activity stack by using `adb shell dumpsys activity` and look at the output. – David Wasser Apr 09 '13 at 16:45
  • You are still right. I have put a logger in `onResume()` method of `Reports` activity to verify that after a `finish()` call in `ReportsType` activity, where its going. Surprisingly, its going to `onResume()` method of `Reports` activity but after that what's causing a call to `MainFragmentActivity` is unknown. However if I start activity intent `Reports` and then call `finish()` as mentioned earlier then everything works fine except then I get a certain delay(a blank screen) and then the view `Reports` activity is shown just fine. – sjain Apr 10 '13 at 07:54
  • Moreover I ran `adb shell dumpsys activity` and got this output: `Running activities (most recent first): TaskRecord{41dd3920 #120 A com.wassap.main U 0} Run #8: ActivityRecord{4194e200 com.wassap.main/.ReportsType} Run #7: ActivityRecord{41aadf20 com.wassap.main/.Reports} Run #6: ActivityRecord{4165f998 com.wassap.main/.MainFragmentActivity} Run #5: ActivityRecord{41b8e060 com.wassap.main/.MainFragmentActivity} Run #4: ActivityRecord{41a93e80 com.wassap.main/.Reports}` – sjain Apr 10 '13 at 08:11
  • If you look at the activity stack from your recent comment, you can see that this is a mess. You have 2 instances of `Reports`, 2 instances of `MainFragmentActivity` and 1 instance of `ReportsType` in your activity stack. This is probably not what you want. You need to have a hard look at how your start your activities. – David Wasser Apr 16 '13 at 14:42
  • I resolved the issue. See my answer below. For the number of instances, I assume that some background process is going too creating multiple instances. – sjain Apr 16 '13 at 14:46

2 Answers2

2

Finally resolved by setting the old intent value in the called activity like this-

@Override
    public void onNewIntent (Intent intent)
    {
        super.onNewIntent(intent);
        setIntent(intent);
    }

Then getting the old intent values at onResume() call-

@Override
    public void onResume()
    {
        super.onResume();
        Intent intent = getIntent();
        heading = intent.getExtras().getString("name");
        TextView heading_txt = (TextView) findViewById(R.id.heading);
        heading_txt.setText(heading);   
    }
sjain
  • 23,126
  • 28
  • 107
  • 185
0

try like in your second Activity

if (getIntent().getExtras() != null) {
        String  mstr = getIntent().getExtras().getString("yourkey");               
        }

and your code

        Intent intent = getIntent();
        heading = intent.getExtras().getString("name"); //this tells which item was clicked.
        TextView heading_txt = (TextView) findViewById(R.id.heading);
        heading_txt.setText(heading);

put in onItemclick where it need not in onCreate

Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45