Navigation durch Activities

  • 1 Antworten
  • Letztes Antwortdatum
B

BlupBlup

Ambitioniertes Mitglied
1
Hi,

ich erstelle eine Navigation durch meine App die von
der normalen standard Navigation abweicht.
Um zu verstehen wie die Implementierung einer
eigenen Navigation funktioniert habe ich mir folgende Seiten druchgelesen

1. Manipulating Android tasks and back stack
2.Tasks and Back Stack | Android Developers

Um mein Problem zu veranschaulichen habe ich
ein kleines Beispielprogramm geschrieben.

Diese App besteht aus drei Activities:
Activity_A (mit zwei Buttons: B und C; android:taskAffinity = com.example.testing )
Activity_B (mit zwei Buttons: A und C; android:taskAffinity = com.testing2)
Activity_C (mit zwei Buttons: A und B; android:tastAffinity = com.testing3)
Contoller (Singleton Pattern)

Die App startet mit Activity_A im Task com.example.testing. Klicke ich auf den Button B startet Activity_B im Task com.testing2. Klicke ich dann auf C
statet Activity_C im Task com.testing3. Drücke ich nun in Acitivty_C auf den Button A oder Button B um zur Activity_A oder Acitivty_B zu wechseln,
wird mir zwar der entsprechende Code in der Controller Klasse ausgeführt,
also der Intent Aufruf, aber trotzdem findet kein Wechsel zu der entsprechenden Activity_A oder Acitivty_B statt.

Wie kann ich das Problem beheben?

Controller Klasse
Code:
package com.example.testing;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;


public class Controller 
{
    private static Controller controller = null;
    private static Activity activity = null;
    
    private Controller()
    {
        super();
    }
    
    
    public static Controller getInstance()
    {
        if(controller == null)
            controller = new Controller();
        
        return controller;
    }
    
    
    public void setActivity(Activity activity)
    {
        if(Controller.activity == null)
            Controller.activity = activity;
    }
    
    
    public void startA()
    {
         activity.runOnUiThread(new Runnable() {

             @Override
             public void run() 
             {
                 Toast.makeText(activity, "A", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(activity, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
             }
         });
    }
    
    
    public void startB()
    {
         activity.runOnUiThread(new Runnable() {

             @Override
             public void run() 
             {
                 Toast.makeText(activity, "B", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(activity, Activity_B.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
             }
         });
    }
    
    
    public void startC()
    {
         activity.runOnUiThread(new Runnable() {

             @Override
             public void run() 
             {
                 Toast.makeText(activity, "C", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(activity, Activity_C.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
             }
         });
    }
}
Activity_A
Code:
package com.example.testing;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class Activity_A extends Activity 
{
    private static String TAG = "Activity_A";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        
        Controller.getInstance().setActivity(this);
        
        Toast.makeText(this, "Activity_A::onCreate()", Toast.LENGTH_SHORT).show();
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    @Override
    protected void onStart()
    {
        super.onStart();
        
        Log.d(TAG, "onStart()");
        Toast.makeText(this, "Activity_A::onStart()", Toast.LENGTH_SHORT).show();
    }
    
    
    @Override
    protected void onRestart()
    {
        super.onRestart();
        
        Log.d(TAG, "onRestart()");
        Toast.makeText(this, "Activity_A::onRestart()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onResume()
    {
        super.onResume();
        
        Log.d(TAG, "onResume()");
        Toast.makeText(this, "Activity_A::onResume()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onPause()
    {
        super.onPause();
        
        Log.d(TAG, "onPause()");
        Toast.makeText(this, "Activity_A::onPause()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onStop()
    {
        super.onStop();
        
        Log.d(TAG, "onStop()");
        Toast.makeText(this, "Activity_A::onStop()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        
        Log.d(TAG, "onDestroy()");
        Toast.makeText(this, "Activity_A::onDestroy()", Toast.LENGTH_SHORT).show();
    }
    
    
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        
        Log.d(TAG, "onNewIntent()");
        Toast.makeText(this, "Activity_A::onNewIntent()", Toast.LENGTH_SHORT).show();
    }
    
    
    public void doSth(View view)
    {
        Controller.getInstance().startB();
    }
    
    
    public void doSth2(View view)
    {
        Controller.getInstance().startC();
    }
}
Activity_B
Code:
package com.example.testing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class Activity_B extends Activity 
{
    private static String TAG = "Activity_B";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        
        Toast.makeText(this, "Activity_B::onCreate()", Toast.LENGTH_SHORT).show();
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    @Override
    protected void onStart()
    {
        super.onStart();
        
        Log.d(TAG, "onStart()");
        Toast.makeText(this, "Activity_B::onStart()", Toast.LENGTH_SHORT).show();
    }
    
    
    @Override
    protected void onRestart()
    {
        super.onRestart();
        
        Log.d(TAG, "onRestart()");
        Toast.makeText(this, "Activity_B::onRestart()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onResume()
    {
        super.onResume();
        
        Log.d(TAG, "onResume()");
        Toast.makeText(this, "Activity_B::onResume()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onPause()
    {
        super.onPause();
        
        Log.d(TAG, "onPause()");
        Toast.makeText(this, "Activity_B::onPause()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onStop()
    {
        super.onStop();
        
        Log.d(TAG, "onStop()");
        Toast.makeText(this, "Activity_B::onStop()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        
        Log.d(TAG, "onDestroy()");
        Toast.makeText(this, "Activity_B::onDestroy()", Toast.LENGTH_SHORT).show();
    }
    
    
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        
        Log.d(TAG, "onNewIntent()");
        Toast.makeText(this, "Activity_B::onNewIntent()", Toast.LENGTH_SHORT).show();
    }
    
    
    public void doSth(View view)
    {
        Controller.getInstance().startA();
    }
    
    
    public void doSth2(View view)
    {
        Controller.getInstance().startC();
    }
}
Activity_C
Code:
package com.example.testing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class Activity_C extends Activity 
{
    private static String TAG = "Activity_C";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c);
        
        Toast.makeText(this, "Activity_C::onCreate()", Toast.LENGTH_SHORT).show();
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    @Override
    protected void onStart()
    {
        super.onStart();
        
        Log.d(TAG, "onStart()");
        Toast.makeText(this, "Activity_C::onStart()", Toast.LENGTH_SHORT).show();
    }
    
    
    @Override
    protected void onRestart()
    {
        super.onRestart();
        
        Log.d(TAG, "onRestart()");
        Toast.makeText(this, "Activity_C::onRestart()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onResume()
    {
        super.onResume();
        
        Log.d(TAG, "onResume()");
        Toast.makeText(this, "Activity_C::onResume()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onPause()
    {
        super.onPause();
        
        Log.d(TAG, "onPause()");
        Toast.makeText(this, "Activity_C::onPause()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onStop()
    {
        super.onStop();
        
        Log.d(TAG, "onStop()");
        Toast.makeText(this, "Activity_C::onStop()", Toast.LENGTH_SHORT).show();
    }

    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        
        Log.d(TAG, "onDestroy()");
        Toast.makeText(this, "Activity_C::onDestroy()", Toast.LENGTH_SHORT).show();
    }
    
    
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        
        Log.d(TAG, "onNewIntent()");
        Toast.makeText(this, "Activity_C::onNewIntent()", Toast.LENGTH_SHORT).show();
    }
    
    
    public void doSth(View view)
    {
        Controller.getInstance().startA();
    }
    
    
    public void doSth2(View view)
    {
        Controller.getInstance().startB();
    }
}
manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testing"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testing.Activity_A"
            android:label="@string/app_name"
            android:launchMode="singleTask" 
            android:taskAffinity="com.example.testing">           
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name="Activity_B"
            android:launchMode="singleTask"
            android:taskAffinity="com.testing2">
        </activity>
        
        <activity
            android:name="Activity_C"
            android:launchMode="singleTask"
            android:taskAffinity="com.testing3">
        </activity>
    </application>

</manifest>
 
Versuch mal in deiner Controller Klasse die Activity der methode startA zu übergeben und mit dieser zu arbeiten. Activityies/Context in Klassen zu speichern ist meiner Erfahrung nach immer eine schlechte Idee.
 
Zurück
Oben Unten