Issue
All,
I am performing a navigation between 3 screens with dependency on each other.
Activity A is having a table with list of table rows. Each row has onclick listener event where in get the data from table row and passes to next screen Activity B.
Activity B starts with reading data from Intent and gets corresponding images & data etc and builds layout. It has a button where in reads data from edit text and passed to Activity C.
Activity C also builds layout based on data from Intent.
I acheived navigation among these activities with Parent activity configuration in Manifest file.
Now when I am doing back navigation in Activity C to Activity B, it's crashing because it could not find data from Intent as the control is coming from Child to Parent rather than Parent to Child.
I understand onCreate function of Activity B is executing again. So I kept onPause() and onResume() method in Activity B to hold the re-execution. But no luck.
Could some one advise how to handle this kind of scenario.
EDIT:
Activity B:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_activity);
getActionBar().setDisplayHomeAsUpEnabled(true);
source_activity = this.getIntent().getStringExtra("source_activity");
Bundle b = this.getIntent().getExtras();
if(b!=null)
DashBoardDisplay_l = b.getParcelable("obj");
Name_ref=DashBoardDisplay_l.getName();
}
Activity C:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale);
setContentView(R.layout.point_log_activity);
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("ref_name");
Log.d(TAG, "ref name "+YourtransferredData);
Name=YourtransferredData;
Log.d(TAG, "name value "+Name);
getActionBar().setDisplayHomeAsUpEnabled(true);
new BackEndJob().execute();
}
Activity B contains some Intent which comes from prior screen Activity A. So while coming back to Activity B from Activity C, it could not find the data from Intent due to which it is resulting to null pointer exception.
So is there any option to display what is there in back navigation stack rather than recreating it with Savedinstance advised by Dalija.
Solution
From your description it looks like back navigation to Activity B should be prevented. So when user presses back button in Activity C it would go back to Activity A. If that is the case then you should finish Activity B when it passes the control to Activity C.
...
this.startActivity(intent); // start Activity C
finish();
}
Finishing activity will remove Activity B from navigation stack and completely destroy Activity B solving your back navigation issue.
However, if you do want to navigate back to Activity B then you should use onSaveInstanceState()
and onRestoreInstanceState()
in Activity B to save its state.
When you launch Activity C, Activity B is stopped. At that point system may decide to destroy Activity B at any time if it needs to clean up resources. You don't have any influence on that process and you cannot prevent it.
You can find more in Activity Lifecycle
Answered By - Dalija Prasnikar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.