Question

I have a tabview which has 5 tab.Each and every is a single activity.In one tab it is opening a new activity which opening a alert.Till now every thing works fine.

My problem is when I am coming back from(Back button press) that child activity it is calling onBackPressed of it's parent activity so I am checking the activity name getLocalActivityManager().getCurrentActivity().toString() it also working when I am pressing back button from child activity, It check the activity name using getLocalActivityManager().getCurrentActivity().toString().

But when I am pressing back button from my parent activity it's giving NullPointer Exception. May be getLocalActivityManager().getCurrentActivity().toString() not working when calling from parent activity

LogCat Error

enter image description here

Parent activity

public class ProgressActivity extends ActivityGroup 
{
private ArrayList<View> history;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progress_tabview);
    this.history = new ArrayList<View>();
}
public void showProgress(View v) 
{
    Intent intMyProg = new Intent(this,ShowProgress.class);
    //intMyProg.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    //startActivity(intMyProg);

    //This will help to open a new activity within existing TabView
    //Need to extend ActivityGroup
    //Need to declare "private ArrayList<View> history;"
    //Need to Define "this.history = new ArrayList<View>();" within onCreate
    View MyView = getLocalActivityManager()
            .startActivity("MyIntent", intMyProg
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
            .getDecorView();


    replaceView(MyView);
}

public void replaceView(View v) 
{
    history.add(v);
    setContentView(v);
}

//@Override  
public void onBackPressed() 
{  
    String activityName = getLocalActivityManager().getCurrentActivity().toString();

    if(activityName.contains("ShowProgress"))
    {
        getCurrentActivity().onBackPressed();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Back button from parent Activity", Toast.LENGTH_SHORT).show();
        //finish();
    }
}
}

Child activity

public class ShowProgress extends ActivityGroup
{
private ArrayList<View> history;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progress);
    this.history = new ArrayList<View>();

    AlertDialog alertDialog = new AlertDialog.Builder(ShowProgress.this.getParent()).create();
    alertDialog.setTitle("Hello ...");
    alertDialog.setMessage("Hello world this is Anirban !");
    alertDialog.setIcon(R.drawable.tick);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            //Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        }
    });
    alertDialog.show();
}

//@Override  
public void onBackPressed() 
{  
    Intent intMyProg = new Intent(this,ProgressActivity.class);
    View MyView = getLocalActivityManager()
            .startActivity("MyIntent", intMyProg
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
            .getDecorView();

    replaceView(MyView);
}

public void replaceView(View v) 
{
    history.add(v);
    setContentView(v);
}
 }
Was it helpful?

Solution

Since you're getting your error in this procedure, the simplest way to get rid of it would be to change it like this

//@Override  

public void onBackPressed() 
{  
    LocalActivityManager localManager = getLocalActivityManager();
    Activity currentActivity = null;
    if (localManager!=null){
          currentActivity = localManager.getCurrentActivity();
    }

    String activityName = null;
    if (currentActivity!=null){
      activityName = currentActivity.toString()
    }

    if(activityName!=null && (activityName.contains("ShowProgress"))
    {
        getCurrentActivity().onBackPressed();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Back button from parent Activity", Toast.LENGTH_SHORT).show();
        //finish();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top